这是我在客户端生成的json。我将这个json发布到服务器上。但是StartDate和EndDate没有被转换。请帮忙。
[
{
"GoalTitle": "Competancy Goal",
"Weightage": 30.5,
"StartDate": "/Date(1388412173070)/",
"EndDate": "/Date(1419948173070)/",
"Status": 0,
"editing": false,
"lstSubGoals": [
{
"GoalTitle": "Competancy Goal - Sub goal",
"Weightage": 31.5,
"StartDate": "/Date(1388412173070)/",
"EndDate": "/Date(1419948173070)/",
"Status": 0,
"editing": false,
"lstSubGoals": []
}
]
},
{
"GoalTitle": "Strategy Goal",
"Weightage": 60.5,
"StartDate": "/Date(1388412173070)/",
"EndDate": "/Date(1419948173070)/",
"Status": 1,
"editing": false,
"lstSubGoals": []
}
]
答案 0 :(得分:1)
var StartDateServer = StartDate;
var parsedDate = new Date(parseInt(StartDateServer.substr(6)));
var finalDate = parsedDate.toLocaleDateString(); //result as mm/dd/yyyy
答案 1 :(得分:0)
在JS文件中添加以下功能:
function ConvertJsonDateString(jsonDate) {
var shortDate = null;
if (jsonDate) {
var regex = /-?\d+/;
var matches = regex.exec(jsonDate);
var dt = new Date(parseInt(matches[0]));
var month = dt.getMonth() + 1;
var monthString = month > 9 ? month : '0' + month;
var day = dt.getDate();
var dayString = day > 9 ? day : '0' + day;
var year = dt.getFullYear();
shortDate = monthString + '-' + dayString + '-' + year;
}
return shortDate;
};
然后你就可以使用它:
<script>
var jsonDate = '/Date(1388412173070)/';
var date = ConvertJsonDateString(jsonDate);
alert(date) // the result will be 12/30/2013
</script>
请在此处查看结果:http://jsfiddle.net/lin/WrcC8/
答案 2 :(得分:0)
function parseJsonDate(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined) parts[2] = 0;
if (parts[3] == undefined) parts[3] = 0;
return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
}
参考文献:here
答案 3 :(得分:0)
<强> JSFIDDLE DEMO 强>
var num = "/Date(1388412591038)/".match(/\d+/g); //regex to extract numbers
var date = new Date(parseFloat(num)); //converting to date
console.log(date.getMonth() + 1 + "-" + date.getDate() + '-' + date.getFullYear());
控制台中的结果: 12-30-2013
答案 4 :(得分:0)
@ dineshd87你可以在jquery中这样做非常简单:
var date = new Date(//the milliseconds here);
var dateString = date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear();