js怎么把时间戳转换为日期格式

2025-02-27 05:00:29
推荐回答(2个)
回答1:

前端有时候可能要从日期控件中拿到日期,然后参与计算,下边记录一个把日期字符串转换成时间戳的小函数。


dateStr格式为“2014-05-08 00:22:11 ”
function get_unix_time(dateStr)
{
var newstr = dateStr.replace(/-/g,'/');
var date =  new Date(newstr);
var time_str = date.getTime().toString();
return time_str.substr(0, 10);

}

回答2:

var time = new Date().getTime();//获取时间戳
console.log(new Date(time).toLocaleString())//时间戳换时间格式