skip to Main Content

I am storing a date in mongodb using mongose with the help of moment js.
I am creating the date object from a date string which is in the format MM/DD/YYYY.
below is how i assign the date

const startDate = momentTz(
    data.startDate,
    "MM/DD/YYYY",
    "Asia/Kolkata"
  ).startOf("day");

but ever time I assign this date object to the mongoose model while creating a document it is storing as 2022-11-30T18:30:00.000+00:00 ie, the time is being automatically set to 18:30.
how can i set this to the start of the day.

2

Answers


  1. MongoDB stores dates as BSON type 9, which stores the number of milliseconds since 1970-01-01 00:00 UTC.

    18:30 UTC is midnight in Kolkata, so that date/time is correctly showing the start of the day 2022-11-31 IST.

    It is up to the client program to convert from UTC to a different timezone if that is desired.

    Login or Signup to reply.
  2. Use this one:

    const startDate = momentTz(
        data.startDate,
        "MM/DD/YYYY",
        "Asia/Kolkata"
      ).startOf("day").toDate();
    

    MongoDB stores Date values as UTC times – always and only. It is the client responsibility to display the date/time in local time zone and format.

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