最近在做微信小程序 寫了個WCF服務,返回實體中有DateTime類型字段 結果序列化后日期變成了 /Date(1494524134000+0800)\ 這種格式 不能正常顯示了 但也不能為了這個吧所有服務的DateTime字段都改成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('/Date(1494524134000+0800)\')
