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

如题所述

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


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);

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-21
var time = new Date().getTime();//获取时间戳
console.log(new Date(time).toLocaleString())//时间戳换时间格式