现在的位置: 首页 > 综合 > 正文

JS日期字符串转时间戳

2017年12月27日 ⁄ 综合 ⁄ 共 566字 ⁄ 字号 评论关闭

JS日期字符串转时间戳

var parseDate = function(str) { // 字符串转时间戳
    return Date.parse(new Date(Date.parse(str.replace(/-/g, "/")))) / 1000; // 精确到秒
};

JS时间戳转日期字符串

var formatDate = function(time) { // 时间戳2字符串
    var time = new Date(parseInt(time)*1000),
        year = time.getFullYear(),
        month = time.getMonth() + 1,
        date = time.getDate(),
        hour = (''+time.getHours()).length > 1 ? time.getHours() : '0'+time.getHours(),
        minute = (''+time.getMinutes()).length > 1 ? time.getMinutes() : '0'+time.getMinutes(),
        second = (''+time.getSeconds()).length > 1 ? time.getSeconds() : '0'+time.getSeconds();
    return year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
};

抱歉!评论已关闭.