0

I've got an edit page with a "date" type input. I'm using node.js, express, and mongoose to pull the content of the input and save it to the database using a date field in a Mongoose model. When I log the date before it goes into the database I'm seeing:2020-03-31. When I look at it in the database I'm seeing:2020-03-31T00:00:00.000+00:00. Does that +00:00 mean it's getting a timezone assigned? Do I need to edit how my MongoDB database is storing dates?

I'm running into trouble with the time zone when I read it back out and use it to populate the input on the edit page. I'm using a Mongoose model function to format the date before it goes into the value field on the html input. Within that function I'm running the below code:

console.log(date);
// Output:
// 2020-03-31T00:00:00.000Z

console.log(date.getDate());
// Output:
// 30

Is the 'Z' coming from the "+00:00" above? I'm GMT-4, so my server is interpreting that time as March 30th at 8pm and now the date picker on the edit page is populated with March 30th instead of the 31st. If I save this, then the next time I load the page the input will read March 29th. I don't want to unintentionally decrement the date by one every time the page is loaded and saved!

In this case, I really don't care about time and just want to refer to the date. Is there a different best practice I can use here?

busoni34
  • 389
  • 1
  • 4
  • 13
  • I recommend viewing this topic: https://stackoverflow.com/questions/3778428/best-way-to-store-date-time-in-mongodb I don't know if it helps 100% of your question. In some tests I did use Chrome, it always saves with the "+00: 00" timezone and converts it to the region of the computer or browser in future use. – Douglas S Jul 21 '20 at 22:03
  • You can save a new date with new Date () and save it using new ``Date(`$ {time-keeping variable} T00: 00: 00.000 + 04: 00`)`` MongoDB will convert the date to timezone 00:00 and when you retrieve it, it will be converted for your region. – Douglas S Jul 21 '20 at 22:06

1 Answers1

1

MongoDB stores timestamps only, it does not have a facility to store dates. Javascript similarly does not have a facility for storing dates natively contrary to what you might expect - the Date type is actually a DateTime. Mongoose also does not have a date type (it uses JS Date which is a datetime).

What this means is:

  • When you are converting user input to a JS Date instance, you are getting the date converted to a datetime (sounds like the time component is zero in UTC in your application).
  • When you are reading the date out of the database, you are reading a datetime (which is the beginning of the day of that date in UTC per your provided information, not in your local time).

I would then proceed as follows:

  • The DateTime you are reading is the beginning of day on your date in UTC.
  • If mongoose converts the time to your local time, as it appears to do, you need to either stop it from doing that or convert back to UTC.
  • Then retrieve the year-month-day components of the UTC datetime.
  • Using these components you can create a new Date instance in local time or do whatever else you wish such as rendering them.

There are various libraries available in JS for dealing with dates and time zones, but the language itself has limited date support.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • 1
    This lead me on the right track. It is actually not mongoose that is converting to local time, it was the javascript Date.getDate() function itself. There is a Date.getUTCDate() (and getUTCFullYear, getUTCMonth, etc.) function that gets those date parts without the local time conversion. Using all of the UTC functions has solved my problem. – busoni34 Jul 21 '20 at 22:51