4
1

More than 3 years have passed since last update.

Node.jsとDynamoDBで日時データの処理

Posted at

DynamoDBに日時データを持たせる2つの方法

  1. データ型をStringにして2016-02-152015-12-21T17:42:34Zのように文字列で持たせる。
  2. データ型をNumberにして1579740176030のように数値で持たせる。

2.項のNumber型の実用例としては、エポック時間 (1970 年 1 月 1 日の 00:00:00 UTC 以降の秒数) を利用することができる。(UNIXTIMEの詳細)

Node.jsでエポック時間を扱う

現在の日時をDateオブジェクトで取得する

const date = new Date();
console.log(date);   // 2020-01-23T01:09:41.444Z
console.log(typeof date);    // 'object'

現在の日時をエポック時間で取得する

const date = Date.now();
console.log(date);   // 1579740176030
console.log(typeof date);    // 'number'

DynamoDBから取得したエポック時間をDateオブジェクトへ変換する

const unixtime = 1579740176030;  // DynamoDBから取得したエポック時間と想定
const date = new Date(unixtime);
console.log(date);   // 2020-01-23T01:09:41.444Z
console.log(typeof date);    // 'object'
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1