(1)把当前时间转成时间戳
//把时间转成时间戳
function timeTampToStr(){
// 当前时间戳
var timestamp = parseInt(new Date().getTime()/1000);
document.write(timestamp);
}
(2)当前时间换日期字符串
//把当前时间转成字符串
function currentTimeChangeStr(){
//获取当前时间
var now = new Date();
//获取当前时间的年份
var yy = now.getFullYear(); //年
//获取当前时间的月份
var mm = now.getMonth() + 1; //月
//获取当前时间的日期
var dd = now.getDate(); //日
//获取当前时间的小时
var hh = now.getHours(); //时
//获取当前时间的分
var ii = now.getMinutes(); //分
//获取当前时间的秒
var ss = now.getSeconds(); //秒
//把上述的时间转成字符串
var clock = yy + "-";
if(mm < 10) clock += "0";
clock += mm + "-";
if(dd < 10) clock += "0";
clock += dd + " ";
if(hh < 10) clock += "0";
clock += hh + ":";
if (ii < 10) clock += '0';
clock += ii + ":";
if (ss < 10) clock += '0';
clock += ss;
//获取当前日期
document.write(clock);
}
(3)日期字符串转时间戳
//日期字符串转成时间戳
//例如var date = '2015-03-05 17:59:00.0';
function dateStrChangeTimeTamp(dateStr){
dateStr = dateStr.substring(0,19);
dateStr = dateStr.replace(/-/g,'/');
var timeTamp = new Date(dateStr).getTime();
document.write(timesTamp);
}
(4)时间戳转日期字符串
//把时间戳转成日期格式
//例如 timeTamp = '1425553097';
function formatTimeTamp(timeTamp){
var time = new Date(timeTamp*1000);
var date = ((time.getFullYear()) + "-" +
(time.getMonth() + 1) + "-" +
(time.getDate()) + " " +
(time.getHour()) + ":" +
(time.getMinutes()) + ":" +
(time.getSeconds());
)
document.write(date);
}