6

I'm using the pikaday date picker plugin (through an angular directive and with momentjs) and sending the value to the server. Converting to json seems to lose a day though:

var d = myPikaObject.getDate();
console.log(d);              // Thu Apr 30 2015 00:00:00 GMT+0200 (SAST)
console.log(d.toJSON());     // 2015-04-29T22:00:00.000Z

I think this is a momentjs problem but I have no idea what's going wrong.

jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • 3
    It isn't losing a day at all. see that GMT+0200? what would the date be if you took away 2 hours? – Kevin B Apr 29 '15 at 15:00
  • I think you may be confusing changing to use a different timezone from losing a day. I had this happen to me due to differences in browser and server timezones. – DrCord Apr 29 '15 at 15:01
  • losing a day = hour difference due to timezones lol. – pgrodrigues Apr 29 '15 at 15:02
  • curses and all I need is the date - I thought T22 was a reference to the timezone lol. Thanks – jcuenod Apr 29 '15 at 15:03
  • `toJSON` outputs an ISO time string. `console.log` outputs a locale time string. That the difference. No time loss. – Leo Apr 29 '15 at 15:07
  • @jcuenod No, `T22:00:00.000` means 22 (hour) 00 (minute) 00 (second) 000 (millisecond). There's no **timezone** in an ISO time string. – Leo Apr 29 '15 at 15:10

2 Answers2

5

It's all about the format of your date.

When you juste print d, you have this :

Thu Apr 30 2015 00:00:00 GMT+0200 (SAST)

It's GMT +2, so when you print d.ToJson() you lost 2 hours. So you are the day before at 22pm

Pierre-Alexandre Moller
  • 2,354
  • 1
  • 20
  • 29
  • Okay, I get the problem. Thank you! How do I get the right date though? – jcuenod Apr 29 '15 at 15:05
  • 1
    Store the timezone as well, then when you parse it later, take the time zone into consideration. Or, don't use `.toJSON()`, use `.toString()` instead. – Kevin B Apr 29 '15 at 15:06
  • 1
    *"How do I get the right date though?"* Always store the date/time in UTC. Only convert to local time for display purposes. – Felix Kling Apr 29 '15 at 15:18
  • @FelixKling I get the logic of what you're saying and under usual circumstances I would but to be honest it seems to me that there are types of dates that shouldn't be localised: a birthday, for example, is just connected to a date not a "localisable" date/time and if you move to New Zealand you just get to have your birthday sooner. – jcuenod Apr 29 '15 at 20:33
3

Giving you're already getting time with momentjs, you can try moment.utc() method. The docs say:

As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc()

moment('2012 juillet', 'YYYY MMM', 'fr');
moment('2012 July',    'YYYY MMM', 'en');

You can do a lot more with the utc() method.

moment.utc();
moment.utc(Number);
moment.utc(Number[]);
moment.utc(String);
moment.utc(String, String);
moment.utc(String, String[]);
moment.utc(String, String, String);
moment.utc(Moment);
moment.utc(Date);
ilter
  • 4,030
  • 3
  • 34
  • 51