DynamoDB error when update Date Unsupported type passed ... Pass options.convertClassInstanceToMaptrue to marshall typeof object as map attribute
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Amazon DynamoDB, when you're working with updates involving Date objects, you might encounter the following error: "Unsupported type passed: Date. Pass options.convertClassInstanceToMap=true to marshall typeof object as map attribute." This article explores the cause of this error and provides guidance on how to resolve it. We'll delve into the specifics of DynamoDB's data handling mechanisms and how they relate to JavaScript objects, followed by practical examples.
Understanding DynamoDB's Data Types
DynamoDB is a NoSQL database that stores data primarily as key-value pairs, where each value is composed of attributes. Each attribute in DynamoDB is strongly typed, and the supported data types are:
- Number
- String
- Binary
- Boolean
- Null
- List
- Map
- Set (String Set, Number Set, Binary Set)
Crucially, DynamoDB does not natively support JavaScript Date objects. Instead, developers must explicitly handle the conversion of Date objects to a format that DynamoDB supports.
The Error Explained
When you attempt to update a Date attribute in your DynamoDB table without appropriate conversion, the AWS SDK throws this error:
This error arises from the fact that DynamoDB's DocumentClient attempts to marshall JavaScript objects to DynamoDB's supported data types. A Date object is a complex type and needs conversion into a format compatible with DynamoDB, typically a string representation, such as ISO 8601.
Technical Explanation
The AWS SDK for JavaScript provides a DocumentClient that automatically serializes objects to DynamoDB's supported attribute types using a process called marshaling. By default, the marshaller doesn't know how to process the JavaScript Date object, which results in the error.
You may use the options.convertClassInstanceToMap flag to signal the marshaller to convert class instances into map attributes. However, this isn't the most efficient or typical approach when dealing with Date objects. Instead, converting the Date object to a string or number is a more standard method.
Converting Date Objects
To handle Date attributes efficiently, consider converting them to ISO 8601 strings or epoch timestamps before sending them to DynamoDB. Here’s how you can do that in JavaScript:
Summary Table
| Problem | Explanation | Solutions |
| Unsupported type passed: Date | DynamoDB does not support Date type and requires data in supported formats. | Convert Date to ISO 8601
string or epoch
timestamp before
updating DynamoDB. |
Additional Considerations
Consistency and Data Formats
When converting dates, maintaining a consistent format across your application is crucial. Using ISO 8601 strings is advantageous due to their human readability and lexicographical order, which is useful during attribute sorting and querying.
Handling Time Zones
Always consider time zones when converting Date objects. The ISO 8601 format, by default, includes the time offset, making it an excellent choice for ensuring consistent time zone references across distributed systems.
Conclusion
Handling Date objects in DynamoDB effectively requires converting them to supported formats such as ISO 8601 strings or epoch timestamps. Doing so ensures seamless integration with DynamoDB's attribute types, preventing encountered errors and facilitating the efficient management of temporal data. By following the best practices outlined in this article, you can effectively avoid the common pitfalls associated with DynamoDB's date handling.
Related reading

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.