项目中碰到了用jQuery从后台获取的json格式的日期的字符串,需要将此字符串转换成JavaScript的日期对象,记在此处,以备后用。
代码
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> // 转换json格式的日期(如:{ServerDatetime:"\/Date(1278930470649)\/"})为Javascript的日期对象
function ConvertJSONDateToJSDateObject(JSONDateString) {
var date = new Date(parseInt(JSONDateString.replace( " /Date( " , "" ).replace( " )/ " , "" ), 10 ));
return date;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> // 转换json格式的日期(如:{ServerDatetime:"\/Date(1278930470649)\/"})为Javascript的日期对象
function ConvertJSONDateToJSDateObject(JSONDateString) {
var date = new Date(parseInt(JSONDateString.replace( " /Date( " , "" ).replace( " )/ " , "" ), 10 ));
return date;
}
function renderTime(data){
var da = eval('new ' + data.replace('/','','g').replace('/','','g'));
return da.getFullYear()+"-"+ da.getMonth()+"-" +da.getDay()+" " +da.getHours()+":"+da.getMinutes() +":"+ da.getSeconds() ;
}