时间与时间戳的相互转换(用时直接复制)

只用最简单,最有效的方法实现之间的相互转换。

时间转时间戳

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp = (Date.parse(new Date(stringTime)))/1000;
console.log(timestamp);        //1404958872

时间戳转时间

function zeroFn(m){return m<10?'0'+m:m};    //补0函数
function getDate(dataTime){
    var time = new Date(dataTime);
    var y = time.getFullYear();  
    var m = time.getMonth()+1;  
    var d = time.getDate();  
    var h = time.getHours();
    var mm = time.getMinutes();  
    var s = time.getSeconds();  
    return y+'-'+zeroFn(m)+'-'+zeroFn(d)+' '+zeroFn(h)+':'+zeroFn(mm)+':'+zeroFn(s);  
};
console.log(getDate(1231231233311));    //2009-01-06 16:40:33
原文地址:https://www.cnblogs.com/chenzhiyu/p/7850381.html