日期与时间戳

1.获取当前日期时间戳
var timestamp = (Date.parse(new Date()))/1000; //获取当前时间的时间戳

2.日期转为时间戳

var endTimeStamp = "2017-09-01 24:00:00";
endTimeStamp = new Date(Date.parse(endTimeStamp.replace(/-/g, "/")));
endTimeStamp = endTimeStamp.getTime();

3.时间戳转为日期

function stampToTime(stamp) {
                var date = new Date(stamp*1000);
                Y = date.getFullYear() + '-';
                M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                D = (date.getDate() <10 ? '0'+ date.getDate() : date.getDate())+ ' ';
                h = (date.getHours()<10 ? '0'+ date.getHours() : date.getHours()) + ':';
                m = (date.getMinutes()<10 ? '0'+ date.getMinutes() : date.getMinutes()) + ':';
                 s = (date.getSeconds()<10 ? '0'+ date.getSeconds() : date.getSeconds());
                 return Y+M+D+h+m;
            }
            var stime = stampToTime(1507215600);
            console.log(stime);  //2017-10-05 23:00:00
打印出来日期格式为 2017-10-05 23:00:00

你可能感兴趣的:(日期与时间戳)