UTC time mojs weird

I am trying to send the asp.net UTC timestamp api as follows:

d = new Date() test = moment(d).utc().valueOf() test2 = moment(d).utc().format("ZZ") utc_asp = "/Date("+test+test2+")/" console.log utc_asp >> /Date(1372670700799+0000)/ 

But the time that the server receives matches the local time?

Or that:

 console.log moment(d) console.log moment(d).utc() D {_i: Mon Jul 01 2013 11:26:10 GMT+0200 (SAST), _f: undefined, _l: undefined, _isUTC: false, _d: Mon Jul 01 2013 11:26:10 GMT+0200 (SAST)…} index.js:39 D {_i: Mon Jul 01 2013 11:26:10 GMT+0200 (SAST), _f: undefined, _l: undefined, _isUTC: true, _d: Mon Jul 01 2013 11:26:10 GMT+0200 (SAST)…} 

:

 console.log moment(now).utc().hour() >> 9 - This is correct! Its 11 - 2, but how come the above 

Am I doing it wrong?

+4
source share
2 answers

To create a moment from the current date, simply use moment() without any parameters.

 var m = moment(); // returns a moment representing "now" 

If you want to format it using the proprietary format /Date()/ for MS, you can use:

 m.format("/[Date](XSSS)/") 

This will give you a value, such as /Date(1372728650261)/ , which is suitable for going into .Net and ultimately will give you a DateTime object where .Kind is Utc .

If you need an extended format with an offset, you can use this:

 m.format("/[Date](XSSSZZ)/") 

And it will return a value to you, e.g. /Date(1372728650261-0700)/ . This complies with the requirements of the DataContractJsonSerializer class in .Net. See the "DateTime Date Format" section in these documents .

However, I highly recommend that you do not use this latter format. It is only recognized by the DataContractJsonSerializer , and the docs explicitly state that any offset you provide will be ignored - it uses its own server offset instead. This is pretty stupid, but what he does. If you use the JavaScriptSerializer class, the offset is also ignored, but it has a different behavior than DCJS (staying with UTC instead of going locally).

In this regard, I would recommend completely abandoning this strange format. Use the ISO8601 standard instead (e.g. 2013-07-01T18:38:29-07:00 ). This is easy to do using momentjs and is a standard format.

 moment().format() // it the default, no need to specify anything. 

On the server side, use JSON.Net , which also uses this default format. And if you really care about the offset, use the DateTimeOffset type on the server instead of the DateTime type.

+2
source

I'm not sure, but try checking to see if your asp.net uses Datetime localtime, utc or unspecified for deserialization.

0
source

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


All Articles