"getDate (). toJSON ()" loses day

I use the pikaday plugin to select the date (via the angular directive and with momentjs ) and send the value to the server. Converting to json seems to be losing 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 problem with the moment, but I have no idea what is happening.

+6
source share
2 answers

By giving you time with momentjs , you can try the moment.utc() method. The docs say:

Starting with version 2.0.0, the locale key can be passed as the third parameter to the 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); 
+3
source

Everything about the format of your date.

When you select d, you get the following:

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

This is GMT +2, so when you print d.ToJson (), you have lost 2 hours. So you eve at 22pm

+5
source

Source: https://habr.com/ru/post/986235/


All Articles