js日期字符串(年月日)与时间戳的转换

1.日期转时间戳

例:日期字符串格式为 2017-12-30

var timestr = date.replace(/-/g,'/');
var timestamp = new Date(timestr).getTime();

2.时间戳转日期

此处时间戳只精确到日

例:时间戳 1514563200000

function add0(m){return m<10?'0'+m:m }
function timestampToString(shijianchuo){
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    return y+'-'+add0(m)+'-'+add0(d);
}  

3.获取当天时间戳

function getTodayTimestamp(){
    var today = new Date();
    var month=today.getMonth()+1;
    month =(month<10 ? "0"+month:month);
    var tdstr = (today.getFullYear()+"/"+month+"/"+today.getDate());
    var date = new Date(Date.parse(tdstr));  
    var tdtsp = Number(date);
    return tdtsp;
}


你可能感兴趣的:(js日期字符串(年月日)与时间戳的转换)