skip to Main Content

I am using timestamps in Mongoose to create ‘createdAt’ and ‘updatedAt’ properties on each data item object. Is it possible to take these dates and format and display them in React? I keep trying with date-fns and I just get invalid date errors.

The time looks like the following in my db: ‘createdAt 2023-11-02T02:55:54.488+00:00’, but looks like this in my code: ‘createdAt: 1698893754488’.

I’m not finding much help in the Mongoose docs so any help would be appreciated!

2

Answers


  1. What you can try:

    Where you are showing your createAt date, you can do like this:

    const date = new Date(createdAt);

    data

    So, new Date() would help.

    Login or Signup to reply.
  2. Yes it is normal,
    your time in Interger is simply your date in linux format
    you can try online for an exemple go to Dan’s Tool

    so in your case the : 1698893754488

    is equal to : GMT: Thursday 2 November 2023 02:55:54.488

    So you can import Moment and only create a new Moment date
    Or using simply the Date.parse()

    And after you can parse a good date format

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search