对于从c#返回的日期字段,当进行json序列化后,在前台js里显示的并不是真正的日期,这让我们感觉很不爽,我们不可能为了这东西,把所有日期字段都变成string吧,所以,找了一个js的扩展方法,来实现这个功能
实现
function changedateformat(jsondate) { jsondate = jsondate.replace(/date(, ).replace()/, ); if (jsondate.indexof(+) > 0) { jsondate = jsondate.substring(0, jsondate.indexof(+)); } else if (jsondate.indexof(-) > 0) { jsondate = jsondate.substring(0, jsondate.indexof(-)); } var date = new date(parseint(jsondate, 10)); var month = date.getmonth() + 1 < 10 ? 0 + (date.getmonth() + 1) : date.getmonth() + 1; var currentdate = date.getdate() < 10 ? 0 + date.getdate() : date.getdate(); return date.getfullyear() + 年 + month + 月 + currentdate + 日 + + date.gethours() + : + date.getminutes();}//调用:changedateformat(data[i].arrdate)
调用
$.ajax({ type: get, texttype: json, url: /userinfo/getuserwithdraw, data: { id: id }, success: function (data) { var result = html.replace(reg, function (node, key) { return { 'money': data.money, 'addtime': changedateformat(data.addtime), 'cashtime': data.cashtime }[key]; }); tsingdatips.ask({ msg: result, show_btn: false, title: 提现申请详情 });//预计打款时间等于申请时音后的(5号或20号) } });
ps:返回的json时间如 /date(1290371638000)/ 形式,怎样处理成 yyyy-mm-dd 这类格式
去掉/date
直接格式化1290371638000
/*** 时间对象的格式化;*/date.prototype.format = function(format){ /* * eg:format=yyyy-mm-dd hh:mm:ss; */ var o = { m+ : this.getmonth()+1, //month d+ : this.getdate(), //day h+ : this.gethours(), //hour m+ : this.getminutes(), //minute s+ : this.getseconds(), //second q+ : math.floor((this.getmonth()+3)/3), //quarter s : this.getmilliseconds() //millisecond } if(/(y+)/.test(format)) { format = format.replace(regexp.$1, (this.getfullyear()+).substr(4 - regexp.$1.length)); } for(var k in o) { if(new regexp((+ k +)).test(format)) { format = format.replace(regexp.$1, regexp.$1.length==1 ? o[k] : (00+ o[k]).substr((+ o[k]).length)); } } return format;}
使用方法:
var testdate = new date();var teststr = testdate.format(yyyy年mm月dd日hh小时mm分ss秒);alert(teststr);
