Akhilesh Pandey
Akhilesh Pandey

Reputation: 66

How to filter date between startdate and enddate in dynamodb which is stored in UTC format

How to filter date between start_date and end_date in dynamodb which is stored in UTC format?

dynamodb.scan({
        TableName : TEMP_TABLE_NAME,
        FilterExpression : 'start_date  between  :start_date and :end_date',
        ExpressionAttributeValues: {
            ":start_date": {
                "S": "2017-08-16"
            },
             ":end_date": {
                "S": "2017-08-15"
            }
        }
    }, function(err, data) {
        console.log(err);
        context.succeed(data);
});

Upvotes: 3

Views: 6391

Answers (1)

Abhaya Chauhan
Abhaya Chauhan

Reputation: 1201

If you store your dates in the correct format, you should be able to perform the BETWEEN operation.

The catch is - DynamoDB does not natively support date/timestamp data types.

You can store your dates / timestamps in either string or number format. AWS Docs

Store as string:

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:

2016-02-15 2015-12-21T17:42:34Z 20150311T122706Z For more information, see http://en.wikipedia.org/wiki/ISO_8601.

Number:

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.

For more information, see http://en.wikipedia.org/wiki/Unix_time.

As long as you stick to the guidelines for whichever data type you go with, this enables you to perform relevant operations on them (ie: BETWEEN) because they will be designed to sort alphanumerically (assuming all are in UTC).

Upvotes: 5

Related Questions