将.NET DateTime转换为JSON

编程入门 行业动态 更新时间:2024-10-25 09:33:43
本文介绍了将.NET DateTime转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 如何格式化JSON日期?

我的web服务将DateTime返回给jQuery调用。该服务返回以下格式的数据:

My webs service is returning a DateTime to a jQuery call. The service returns the data in this format:

/Date(1245398693390)/

如何将其转换为JavaScript友好的日期?

How can I convert this into a JavaScript-friendly date?

推荐答案

返回的是从时代开始的毫秒。你可以这样做:

What is returned is milliseconds since epoch. You could do:

var d = new Date(); d.setTime(1245398693390); document.write(d);

关于如何根据需要格式化日期,请参阅完整的 Date 参考资料,网址为 www.w3schools/jsref/jsref_obj_date.asp

On how to format the date exactly as you want, see full Date reference at www.w3schools/jsref/jsref_obj_date.asp

您可以通过解析整数来剥离非数字(如这里所示):

You could strip the non-digits by either parsing the integer (as suggested here):

var date = new Date(parseInt(jsonDate.substr(6)));

或应用以下正则表达式(来自Tominator在评论中):

Or applying the following regular expression (from Tominator in the comments):

var jsonDate = jqueryCall(); // returns "/Date(1245398693390)/"; var re = /-?\d+/; var m = re.exec(jsonDate); var d = new Date(parseInt(m[0]));

更多推荐

将.NET DateTime转换为JSON

本文发布于:2023-11-04 12:15:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   NET   DateTime   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!