Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - 将.NET DateTime转换为JSON [重复](Converting .NET DateTime to JSON [duplicate])

Possible Duplicate:(可能重复:)

My webs service is returning a DateTime to a jQuery call.(我的网站服务正在将一个DateTime返回给jQuery调用。)

The service returns the data in this format:(该服务以以下格式返回数据:) /Date(1245398693390)/ How can I convert this into a JavaScript-friendly date?(如何将其转换为适合JavaScript的日期?)   ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What is returned is milliseconds since epoch.(返回的是自纪元以来的毫秒数。)

You could do:(你可以这样做:) var d = new Date(); d.setTime(1245398693390); document.write(d); On how to format the date exactly as you want, see full Date reference at http://www.w3schools.com/jsref/jsref_obj_date.asp(有关如何根据需要格式化日期的信息,请参阅http://www.w3schools.com/jsref/jsref_obj_date.asp上的完整Date参考) You could strip the non-digits by either parsing the integer ( as suggested here ):(您可以通过解析整数来剥离非数字( 如此处所示 ):) var date = new Date(parseInt(jsonDate.substr(6))); Or applying the following regular expression (from Tominator in the comments):(或者应用以下正则表达式(来自评论中的Tominator):) var jsonDate = jqueryCall(); // returns "/Date(1245398693390)/"; var re = /-?d+/; var m = re.exec(jsonDate); var d = new Date(parseInt(m[0]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...