DynamoDBに日時データを持たせる2つの方法
- データ型を
String
にして2016-02-15
や2015-12-21T17:42:34Z
のように文字列で持たせる。 - データ型を
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'