js格式化处理json时间问题。。。

暴走的车轮 2010-12-09 05:44:21
返回的json时间如 /Date(1290371638000)/ 形式,怎样处理成 yyyy-MM-dd 这类格式啊????用js
...全文
911 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahopedog 2010-12-09
  • 打赏
  • 举报
回复
var times = 1290371638000;
var date = new Date(times);
alert(date.toLocaleString());
ahopedog 2010-12-09
  • 打赏
  • 举报
回复
var times = 1290371638000;
var date = new Date(times);
alert(date.toLocaleString);
hch126163 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xming4321 的回复:]

去掉/Date
直接格式化1290371638000


/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format){
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" : this.getMonth()+1, //month
……
[/Quote]
正解
十一文 2010-12-09
  • 打赏
  • 举报
回复
去掉/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);

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧