Home > Mobile >  Can't store date as start of the day in mongoose
Can't store date as start of the day in mongoose

Time:12-19

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.

CodePudding user response:

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.

CodePudding user response:

Mongoose does not store timezone information by default.

Try to use use toString() method before sending data.

Date().toString()
  • Related