How to deal with user input of dates using ISODate stored to Mongo db?

I’m running into issues working with dates/times with mongo and autoform.

I started with a migration from another DB system, where I had dates stored as strings.

the migration was in PHP and I used the following to convert to Mongo dates:

return new MongoDate(strtotime($date));

So, for example, I start with 7/30/1960 (july 30, 1960)

And In Mongodb, I see:

ISODate(“1960-07-30T05:00:00.000+0000”),

And, when I view that in my table grid using a helper and the moment library:

Template.registerHelper("mdy", function (date) {
  if (date) {
    return moment(date).format('MM/DD/YYYY');
  }
});

I get:
07/30/1960

Then, I open that up in a form, using autoForm with a schema like this:

someDate: {
type: Date,
}

and still, I see 07/30/1960

but then I save that to the database, and look back in the database to see what is saved:

ISODate("1960-07-30T00:00:00.000+0000"),

(Note it has changed from T05 to T00)

and now when I display that in my table grid I see:

07/29/1960

But interestingly, if I open that record again in the autoform, it shows 07/30/1960

So there is obviously something going wrong with timezone, or GMT offset, or something like that

Anyone know what I’m missing?

To simply this a bit, I have created a standalone test app that illustrates the issue. If you could, please try it and let me know what you think. I’m definitely having an issue with timezone.

I would think this is a common use case to let the user pick a date using a date popup with Autoform, and save it to the mongodb, then display it back to the user in a table. Not sure why I can’t find a good example of this, or why I can’t make it work myself !! :smile:

screenshots of entering the data of 7/1/2015:

but then it displays as Tue Jun 30 2015 19:00:00 GMT-0500 (CDT):

Seems like you are getting UTC and expecting localized time (timezonedddd). I’ll pull the repo and take a look later today (unless someone else jumps in)

:slight_smile:

to tell the truth, I’m really just looking to store a Date, I don’t really need the extra confusion of the time component… but it seems the only way (other than storing a String) is the way I’m doing it… (and I can’t store a string because I really want to be able to do native date sorting with my Mongo queries) Thank you for looking at it. I tried using meteor pad but it didn’t give me the date popup on the form, but here it is in case it works for someone else:
http://meteorpad.com/pad/FXr4TYQdZbRDRZxLa/Date%20Issue%20Demo

im on a call currently, but will take a look soon. the timezone issue does have some relevance since UTC could put you ahead-in-time day wise

your schema has the pubdate type as date, so its not setting the time component

i’ve not used autoform or simple schema packages, but this seems relevant to what you are seeing

I only want to store a date, I’m fine with 00:00:00 for the time, if it needs to be on there, the thing is, something is being adjusted per the GMT offset and thus, I am getting a 5-6 hour shift, and thus displaying the wrong day … I tried using datetime-local, and that just allowed the user to select a time, but I don’t need to do that, a date of birth doesn’t need time in my system.

SOLVED!:
thanks to the first half of this post:

which explains my exact issue with the shift, I learned to use moment.utc(date) instead of moment(date) to format my dates and now it shows correctly.

Template.registerHelper(“mdy”, function (date) {
if (date) {
return moment.utc(date).format(‘MM/DD/YYYY’);
}
});

3 Likes