How to store Dates or Timestamps in DynamoDB
DynamoDB does not natively support date/timestamp data types.
It sucks – I know. It leads to a lot of confusion. A question I see over and over again is how do you store your dates or timestamps. This is just one example.
The answer is not as straight forward as you’d hope either, because you have two options to assess.
It comes down to the format you prefer to store your date or timestamps in.
A key criteria in these options is you want the ability to leverage sorting, and comparison operators in DynamoDB, like Greater Than, Less Than, Between, and Sorting etc.
If you store your dates / timestamps as ISO 8601 format, you should use String.
If you store your dates / timestamps in Epoch format, you should use Number.
DynamoDB collates and compares strings using the bytes of the underlying UTF-8 string encoding. For example, “a” (0x61) is greater than “A” (0x41), and “¿” (0xC2BF) is greater than “z” (0x7A).
You can use the string data type to represent a date or a time stamp. One way to do this is by using ISO 8601 strings, as shown in these examples:
You can use the number data type to represent a date or a time stamp. One way to do this is by using epoch time—the number of seconds since 00:00:00 UTC on 1 January 1970. For example, the epoch time 1437136300 represents 12:31:40 UTC on 17 July 2015.
String and ISO 8601 is more human readable (easier when dealing with the data directly, ie: Support), but keep in mind, the Time to Live feature uses Epoch time, so if you have any chance of using that feature, I would recommend the Number approach.