JSON

Javascript Date and JSON DateTime

字号+ 作者:H5之家 来源:H5之家 2017-06-13 14:04 我要评论( )

Javascript Date and JSON DateTime-程序代码库-The program code library

收录日期:2017/06/13 14:04:52 时间:2009-10-30 01:27:39 标签:javascript,json,datetime,date

I have a problem with Jquery function getJSON, the action url does not trigger because one of the parameter i am passing is a javascript date but the action expects c# DateTime..

Is it possible to format the Javascript Date to make it compatible for c# DateTime?

I would suggest using the Datejs library (). From my limited experience with it it's fantastic.

Use this function taken from the :

/* use a function for the exact format desired... */ function ISODateString(d){ function pad(n){return n<10 ? '0'+n : n} return d.getUTCFullYear()+'-' + pad(d.getUTCMonth()+1)+'-' + pad(d.getUTCDate())+'T' + pad(d.getUTCHours())+':' + pad(d.getUTCMinutes())+':' + pad(d.getUTCSeconds())+'Z' }

.NET will have no problem handling an ISO formatted date. You can use DateTime.Parse(...) to handle the ISO formatted string.

If you are trying for a solution to get a Javascript date from the JSON representation (/Date(1350035703817)/) you can use :

function parseJsonDate(jsonDate) { var offset = new Date().getTimezoneOffset() * 60000; var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate); if (parts[2] == undefined) parts[2] = 0; if (parts[3] == undefined) parts[3] = 0; return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000); };

Worked for me like charm.

I used this function, shorter than the above one.

function ParseJsonDate(dateString) { var milli = dateString.replace(/\/Date\((-?\d+)\)\//, '$1'); var date = new Date(parseInt(milli)); return date; }

Also found a method to convert them back:

function ToJsonDate(date) { return '\/Date(' + date.getTime() + ')\/'; }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android开发学习之路-网络编程之xml、json

    Android开发学习之路-网络编程之xml、json

    2017-06-13 13:03

  • Android通过json向MySQL中读写数据的方法详解【写入篇】

    Android通过json向MySQL中读写数据的方法详解【写入篇】

    2017-06-11 16:06

  • 使用JSON的Android身份验证

    使用JSON的Android身份验证

    2017-06-06 09:32

  • JSON基础知识与Javascript解析方式分析

    JSON基础知识与Javascript解析方式分析

    2017-06-03 09:02

网友点评