strdate = "2015-08-09 08:01:36:789";
var d = newDate(strdate);
var timestamp=Math.round(d.getTime());
document.write("timestamp: "+timestamp+"<br>");
输出:
timestamp: 1439078496789
其他方法:
var timestamp = Date.parse(strdate);
或者
var timestamp = d.valueOf();
注意:
strdate要加上毫秒,不然再转换成datetime会丢失毫秒
timestamp转换成datetime
functiontimeStamp2String(time){var datetime = newDate();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1;
var date = datetime.getDate();
var hour = datetime.getHours();
var minute = datetime.getMinutes();
var second = datetime.getSeconds();
var mseconds = datetime.getMilliseconds();
return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second+"."+mseconds;
};
var time = "1439078496789"; //or time=1439018115000; 结果一样var strdate2 = timeStamp2String(time);
document.write("date: "+strdate2+"<br>");
输出:
date: 2015-8-98:1:36.789