標(biāo)簽:js timestamp datetime 轉(zhuǎn)換 javascript 1. datetime轉(zhuǎn)換成timestampstrdate = "2015-08-09 08:01:36:789";
var d = new Date(strdate);
var timestamp=Math.round(d.getTime());
document.write("timestamp: "+timestamp+"<br>");
輸出:
timestamp: 1439078496789
其他方法:
var timestamp = Date.parse(strdate);
或者
var timestamp = d.valueOf();
注意:
strdate要加上毫秒,不然再轉(zhuǎn)換成datetime會丟失毫秒
2. timestamp轉(zhuǎn)換成datetime
function timeStamp2String (time){
var datetime = new Date();
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; 結(jié)果一樣
var strdate2 = timeStamp2String(time);
document.write("date: "+strdate2+"<br>");
輸出:
date: 2015-8-9
8:1:36.789 原文:http://blog.csdn.net/xxm524/article/details/47373089 /*將timestamp 轉(zhuǎn)換成 newdate*/ function timeStampToString (time){ var datetime = new Date(); datetime.setTime(time); return datetime.Format("yyyy-MM-dd hh:mm:ss"); }; /*將newdate時間格式化*/ Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } js不提供直接取unix timestamp的戳記,但是可以用一個簡單的語句來實(shí)現(xiàn)
get.Time()取毫秒數(shù),,所以要取1000 Math.round是初始化一個數(shù)學(xué)方法,類似于php的ceil方法,。 timestmp轉(zhuǎn)Date: Date.prototype.format = function(format) { |
|