1、时间戳差转化为时间
var data1 = Date.parse(new Date()) //当前时间戳
var data2 = (new Date(2018, 6, 6, 0, 0, 0)).getTime(); //7月6日的时间戳
function getDifValue(nowtimestamp, beforetimestamp){
var difValue = nowtimestamp - beforetimestamp;
var day = Math.floor(difValue / 1000 / 60 / 60 / 24);//天
difValue = difValue % (1000 * 60 * 60 * 24);
var hour = Math.floor(difValue / 1000 / 60 / 60);//小时
difValue = difValue % (1000 * 60 * 60);
var min = Math.floor(difValue / 1000 / 60);//分钟
difValue = difValue % (1000 * 60);
var second = Math.floor(difValue / 1000);
return "超时:" + day + "年" + hour + "小时" + min + "分钟" + second + "秒"
}
getDifValue(data1,data2);
2、日期转化为时间戳
var
date =
new
Date(
'2014-04-23 18:55:49:123'
);
// 有三种方式获取
var
time1 = date.getTime();
var
time2 = date.valueOf();
var
time3 = Date.parse(date);
function
timestampToTime(timestamp) {
var
date =
new
Date(timestamp * 1000);
//时间戳为10位需*1000,时间戳为13位的话不需乘1000
var
Y = date.getFullYear() +
'-'
;
var
M = (date.getMonth()+1 < 10 ?
'0'
+(date.getMonth()+1) : date.getMonth()+1) +
'-'
;
var
D = date.getDate() +
' '
;
var
h = date.getHours() +
':'
;
var
m = date.getMinutes() +
':'
;
var
s = date.getSeconds();
return
Y+M+D+h+m+s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804));
//2014-06-18 10:33:24
4、2020-08-08与2020/08/08获取时间戳有差值,前者实际时间是2020-08-08 08:00:00,后者为2020/08/08 00:00:00