Proper Way to Convert JSON Date to .NET DateTime During Deserialization

前端 未结 6 1282
暖寄归人
暖寄归人 2020-12-06 01:12

I have a javascript function that calls an MVC controller with JSON data:

var specsAsJson = JSON.stringify(specs);
$.post(\'/Home/Save\', { jsonData: specsAs         


        
6条回答
  •  醉话见心
    2020-12-06 01:49

    One thing that catches people out quite often with converting between Javascript dates and various server-side languages is that although both sides may be able to understand a unix-style timestamp value, JS uses microsecond-precision timestamp, whereas in most other languages the default timestamp precision is to the second.

    In other words, 1347993132851 in Javascript needs to be divided by 1000 in order to be recognised as a unix timestamp in other languages.

    Alternatively, if your platform can accept formatted date strings, use the Javascript Date() object to convert a timestamp value into a formatted date to send to the server. Or even better, use a helper library such as Date.js or Moment.js.

提交回复
热议问题