标签:
1 // val为经json直接序列化后的C#的DateTime类型的数据
2 function formatTime(val) {
3 var re = /-?\d+/;
4 var m = re.exec(val);
5 var d = new Date(parseInt(m[0]));
6 // 按【2012-02-13 09:09:09】的格式返回日期
7 return d.format("yyyy-MM-dd hh:mm:ss");
8 }
9
10 Date.prototype.format = function (format)
11 {
12 var o = {
13 "M+": this.getMonth() + 1, //month
14 "d+": this.getDate(), //day
15 "h+": this.getHours(), //hour
16 "m+": this.getMinutes(), //minute
17 "s+": this.getSeconds(), //second
18 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
19 "S": this.getMilliseconds() //millisecond
20 }
21 if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
22 (this.getFullYear() + "").substr(4 - RegExp.$1.length));
23 for (var k in o) if (new RegExp("(" + k + ")").test(format))
24 format = format.replace(RegExp.$1,
25 RegExp.$1.length == 1 ? o[k] :
26 ("00" + o[k]).substr(("" + o[k]).length));
27 return format;
28 };
主要用于后台数据返回时,时间格式的转换
完整的代码如下
1 //由图书编号进行实时搜索
2 $(function () {
3 $("#BookId").bind("input onpropertychange", function () {
4 var value = $("#BookId").val();
5 $.ajax({
6 url: ‘/BookAdmin/Return_UpBookInfo‘,
7 type: ‘post‘,
8 data: {
9 ‘BookId‘: $("#BookId").val()
10 },
11 dataType: ‘json‘,
12 success: function (data) {
13 if (data == null) {
14 $("#BookName").attr("value", "");
15 $("#Author").attr("value","");
16 $("#Translator").attr("value", "");
17 $("#Price").attr("value", "");
18 $("#ISBNCode").attr("value", "");
19 $("#PublishCompany").attr("value","");
20 $("#ComeUpTime").attr("value", "");
21 $("#State").attr("value", "");
22 $("#EnteringMen").attr("value", "");
23 }
24 else {
25 $("#BookName").attr("value", data.BookName);
26 $("#Author").attr("value", data.Author);
27 $("#Translator").attr("value", data.Translator);
28 $("#Price").attr("value", data.Price);
29 $("#ISBNCode").attr("value", data.ISBNCode);
30 $("#PublishCompany").attr("value", data.PublishCompany);
31
32 $("#ComeUpTime").attr("value", formatTime(data.ComeUpTime));
33 $("#State").attr("value", data.State);
34 $("#EnteringMen").attr("value", data.EnteringMen);
35 }
36 },
37 error: function (data) {
38 }
39 })
40 });
41
42 // val为经json直接序列化后的C#的DateTime类型的数据
43 function formatTime(val) {
44 var re = /-?\d+/;
45 var m = re.exec(val);
46 var d = new Date(parseInt(m[0]));
47 // 按【2012-02-13 09:09:09】的格式返回日期
48 return d.format("yyyy-MM-dd hh:mm:ss");
49 }
50
51 Date.prototype.format = function (format) //author: meizz
52 {
53 var o = {
54 "M+": this.getMonth() + 1, //month
55 "d+": this.getDate(), //day
56 "h+": this.getHours(), //hour
57 "m+": this.getMinutes(), //minute
58 "s+": this.getSeconds(), //second
59 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
60 "S": this.getMilliseconds() //millisecond
61 }
62 if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
63 (this.getFullYear() + "").substr(4 - RegExp.$1.length));
64 for (var k in o) if (new RegExp("(" + k + ")").test(format))
65 format = format.replace(RegExp.$1,
66 RegExp.$1.length == 1 ? o[k] :
67 ("00" + o[k]).substr(("" + o[k]).length));
68 return format;
69 };
70 });
Json的DataTime格式在Javascript中的转换
标签:
原文地址:http://www.cnblogs.com/yuzhou6/p/5106766.html