js中日期格式与时间戳格式互换

2014-04-23 18:55:49:123    日期格式

1398250549123        时间戳格式

前台显示日期格式,则

function tsToTime(ts) {

        var date = new Date(ts * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
}
tsToTime(1403793804);
console.log(tsToTime(1403793804));

  

 
 
将时间格式的字符串转换为时间戳传给后台,则
 
    
    var date = new Date('2018-11-28 02:47:30:097');
    // 有三种方式获取
    var time1 = date.getTime();
    var time2 = date.valueOf();
    var time3 = Date.parse(date);
    console.log(time1);
    console.log(time2);
    console.log(time3);

  

猜你喜欢

转载自www.cnblogs.com/anqiang1995/p/10034500.html
今日推荐