Is it possible to save datetime to DynamoDB?

PythonDatetimeAmazon Web-ServicesAmazon DynamodbBoto

Python Problem Overview


I have the next code:

users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
users_table.put_item(data={
  "login": login,
  "password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
  "profile": profile,
  "registration_date": datetime.now() # PROBLEM IS HERE
})

But when I run it, it fails with error:

> TypeError: Unsupported type "< type 'datetime.datetime' >" for value "2015-01-12 05:02:57.053131"

I've tried a lot of ways, but it seems that it isn't possible to save datetime to DynamoDB. Btw it works fine in MongoDB.

Is there any solution?

Python Solutions


Solution 1 - Python

Okay, I see that DynamoDB does not support any date types. So the only solution is to use unix-like time as integer, or save date as string.

enter image description here

Solution 2 - Python

According to alejandro-franco response .isoformat() make the trick.

Just tested and this a working example:

CustomerPreferenceTable.put_item(
    Item={
        "id": str(uuid4()),
        "validAfter": datetime.utcnow().isoformat(),
        "validBefore": (datetime.utcnow() + timedelta(days=365)).isoformat(),
        "tags": ["potato", "eggplant"]
    }
)

Solution 3 - Python

According to the documentation: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaSDKHighLevel.html

> Date S (string type). The Date values are stored as ISO-8601 formatted > strings.

Solution 4 - Python

old post but maybe still interesting ..

What you can do and how it worked for me:

import datetime
from datetime import datetime

...

now = datetime.now()
x = now.strftime("%m/%d/%Y, %H:%M:%S")

 table.put_item(
           Item={
               'Index': Index,
           
               'Stamp': x,
               
            }
        )

And with adaption to the code above:

import datetime
from datetime import datetime

...

now = datetime.now()
x = now.strftime("%m/%d/%Y, %H:%M:%S")

users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
users_table.put_item(data={
  "login": login,
  "password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
  "profile": profile,
  "registration_date": x, 
})   

My Output

You can see the format in the DB

Solution 5 - Python

Reading the documentation lately, I found the right link to the documentation here. The recommended way to store date and time data in DynamoDB is using ISO 8601 strings. so the data type is just string.

Solution 6 - Python

I'm not sure why datetime isn't supported in DynamoDB, or in fact I have no experience in it neither.

But if you're so insisted in not converting the datetime to string like people suggested, you can convert the datetime to timestamp, and so you can compare with it.

updated

And you may want to read this SO Question, seemed like numeric comparison is the preferred way.

Solution 7 - Python

If you want to use date to find users, you can simply invoke date() function. Like this:

...
users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
current = datetime.now()
users_table.put_item(data={
  "login": login,
  "password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
  "profile": profile,
  # here use a different name for the entry
  "registration_time": current
  "registration_date": current.date()
})
...

Solution 8 - Python

These are all the supported types for attribute values in DynamoDB as listed in their AWS Docs.

> B A Binary data type. > > Type: Blob > > Required: No > > BOOL A Boolean data type. > > Type: Boolean > > Required: No > > BS A Binary Set data type. > > Type: array of Blobs > > Required: No > > L A List of attribute values. > > Type: array of AttributeValue objects > > Required: No > > M A Map of attribute values. > > Type: String to AttributeValue object map > > Required: No > > N A Number data type. > > Type: String > > Required: No > > NS A Number Set data type. > > Type: array of Strings > > Required: No > > NULL A Null data type. > > Type: Boolean > > Required: No > > S A String data type. > > Type: String > > Required: No > > SS A String Set data type. > > Type: array of Strings > > Required: No

Solution 9 - Python

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander PerechnevView Question on Stackoverflow
Solution 1 - PythonAlexander PerechnevView Answer on Stackoverflow
Solution 2 - Pythonuser1855042View Answer on Stackoverflow
Solution 3 - PythonFred CamposView Answer on Stackoverflow
Solution 4 - PythondvoelkerView Answer on Stackoverflow
Solution 5 - Pythonalejo4373View Answer on Stackoverflow
Solution 6 - PythonAnzelView Answer on Stackoverflow
Solution 7 - PythonStephen LinView Answer on Stackoverflow
Solution 8 - PythonH6.View Answer on Stackoverflow
Solution 9 - PythonRuelos JoelView Answer on Stackoverflow