Skip to content

Convert Epoch time to human readable time in DynamoDB

0

Hi, My application enters a record for every transaction into the dynamoDB table. But it is entered as the epoch time. How can I convert into a human readable time?

asked 2 years ago378 views

1 Answer
2

To convert Epoch time to a human-readable format in DynamoDB, you have a few options:

  1. Client-side conversion: You can retrieve the Epoch time from DynamoDB and convert it to a human-readable format in your application code. Most programming languages have built-in functions or libraries to handle this conversion.

  2. DynamoDB Streams with Lambda: You can use DynamoDB Streams in conjunction with AWS Lambda to automatically convert the Epoch time to a human-readable format as data is inserted or modified in your table.

  3. AppSync resolvers: If you're using AWS AppSync with DynamoDB, you can utilize the util.time helpers in your resolvers to convert Epoch time to various human-readable formats.

Here's an example of how you might convert Epoch time to a human-readable format using JavaScript:

// Assuming you have retrieved the Epoch time from DynamoDB
const epochTime = 1623456789; // Example Epoch time

// Convert to a JavaScript Date object
const date = new Date(epochTime * 1000); // Multiply by 1000 as JavaScript uses milliseconds

// Format the date as a string
const humanReadableTime = date.toISOString();

console.log(humanReadableTime); // Output: 2021-06-11T18:19:49.000Z

You can adjust the formatting to suit your needs, such as using toLocaleString() for a more localized format.

If you want to perform this conversion directly in DynamoDB, you would need to modify your data insertion process to include both the Epoch time and the human-readable time as separate attributes. This could be done in your application code before writing to DynamoDB, or by using a Lambda function triggered by DynamoDB Streams to add the converted time to your items after they're inserted.

Remember that storing both Epoch time and human-readable time can be beneficial, as Epoch time is useful for sorting and calculations, while human-readable time is better for display purposes.
Sources
Time helpers in util.time - AWS AppSync

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.