Storing datetime in JSON file

Note: This post is a details-post for the post: Learning web app development through free online tutorials – Organized Notes and Log, https://raviswdev.blogspot.com/2024/03/learning-web-app-development-through.html .

For storing datetime in JSON file I first tried out following in node program in console:
console.log(JSON.stringify(new Date()));
It gave the output similar to what is given below (but I had done it a little earlier and so time was a little earlier)
"2024-01-28T09:02:30.882Z"

I used this in the JSON data file as follows:
const posts = [
  {
    id: 1,
    datetime: "2024-01-28T08:49:12.133Z",
    title: "Test Post 1 title",
    body: "Test Post 1 body",
  },
...
]
...

Then in the React component I used this code to display the datetime:
              <p>Post datetime: {new Date(post.datetime).toString()}</p>
This code displayed:
Post datetime: Sun Jan 28 2024 14:19:12 GMT+0530 (India Standard Time)

Ref: “2. Convert the date into an ISO 8601 Date string” in JSON date format: 3 ways to work with dates in JSON, https://jsoneditoronline.org/indepth/parse/json-date-format/ 

Comments

Archive