Record one I encountered. The post request in MVC returns a JSON string containing the time format in the database (such as: /Date(10000000000)/). I don't know how to deal with it.
Baidu's methods are not applicable. After researching it myself, it has made a Jquery plug-in, hoping it will be helpful to everyone.
Plugin source code:
(function ($) {/Format the date type returned by JSON is the format defined by itself: For example: yyyy-MM-dd hh:mm:ssdtstr: The date returned by JSON"/Date(1000000000)/"* fmt: Custom format, such as: yyyy-MM-dd hh:mm:ss/$.Format = function (dstr,fmt) {var date = $.ToDateTime(dstr);var o = {"M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+": date.getHours(), //hour "H+": date.getHours(), //hour "m+": date.getMinutes(), //min "s+": date.getSeconds(), //second "q+": Math.floor((date.getMonth() + 3) / 3), //Quarterly "S": date.getMilliseconds() //milliseconds};if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));for (var k in o)if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));return fmt;}/Format JSON returns date type LONG to date dtstr: JSON returns date "/Date(10000000000)/"/$.ToDateTime = function (dstr) {//1900-01-01T00:00:00if ((dstr + '').indexOf('Date(') >= 0) {var ldate = +(dstr + '').replace('/Date(', '').replace(')/', '');if (ldate > 0) { return new Date(ldate); }}else {var str = (dstr + '').replace(/-/g, "/").replace(/T/g, " ");if (str.split('.')[0] == '1900/01/01 00:00:00') {return null;}else if ((dstr + '').length > 0) {return new Date(str.split('.')[0]);}}return null;}/Format JSON Returns Date Type LONG to specified format string: 2015-01-01 12:59:59title: Date type name "Publish time" dtstr: JSON Returns Date "/Date(10000000000)/" substrstart: String start truncation position "5"/$.ToDateTimeString = function (title, dstr, substrstart) {var dt = toDateTime(dstr);if (dt) {var y = dt.getFullYear();var m = dt.getMonth() + 1;var d = dt.getDate();var h = dt.getHours();var i = dt.getHours();var s = dt.getHours();var datestr = y + "-" + (m < 10 ? "0" : "") + m + "-" + (dt.getDate() < 10 ? "0" : "") + dt.getDate() + " " + (dt.getHours() < 10 ? "0" : "") + dt.getHours() + ":" + (dt.getMinutes() < 10 ? "0" : "") + dt.getMinutes() + ":" + (dt.getSeconds() < 10 ? "0" : "") + dt.getMinutes() + ":" + (dt.getSeconds() < 10 ? "0" : "") + dt.getSeconds(); return title + datestr.substr(substrstart);} return "";}/Format JSON Return Date Type Description: LONG type date to the specified format string, find the date and time difference, and append to the specified set: 2015-01-01 12:59:59htmls: Target set title: Date type name "Publish time" dtstr: JSON returns date "/Date(100000000000)/"dtstr01: JSON returns date "/Date(10000000000)/"time difference end date 1dtstr02: JSON returns date "/Date(10000000000)/"time difference end date 2 If 1substrstart is empty: The string starts truncation position "5"*/$.ToDateTimeDiffString = function (htmls, title, dtstr, dtstr01, dtstr02, substrstart) {var dtstrs = toDateTimeString("", dtstr, substrstart);if (dtstrs) {var dt1 = toDateTime(dstr);var dt2 = toDateTime(dstr01);var dt3 = toDateTime(dstr02);if (dt3) { dt2 = dt3; }var tstime = dt1 - dt2;var m = Math.floor(tstime / 1000 / 60, 0);var s = Math.ceil(tstime / 1000 % 60);htmls.push('' + title + (m > 0 ? m + "minutes" : "") + (s > 0 ? s + "seconds" : "") + " " + dtstrs + '');}}})(jQuery);Time formatting jquery pluginHow to use
1. Quote the above jquery plugin
2. Format time:
$.post("/tigger/GetUserPrizeLog?t=" + Math.random(), { activityCode: 1138, myOpenId: "@WorkContext.WxUserInfo.OpenId" }, function (json) { var str = ""; $.each(json.Body, function (i, model) { str += "<li>"; str += " <p class=/"title/">" + model.ActivityPrizeName + "</p>"; str += "<p class=/"time/">" + $.Format(model.CreateTime, "yyyy-MM-dd hh:mm") + "</p>"; str += "<span class=/"get/" onclick=/"javascript:ShowPrize('" + model.ActivityPrizeName + "')/"></span>"; str += "</li>"; }); $("#record ul").html(str); });model.CreateTime is the time obtained from the server
How to use:
$.Format(model.CreateTime, "yyyy-MM-dd hh:mm")
The above article in-depth understanding of the time js formatting in MVC is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.