<html>
<script>
let timeStr = '20160101060000'
function addtime(reqTime) {
// 将你的格式转换成时间格式
reqTime = reqTime.substr(0, 4) + '-' + reqTime.substr(4, 2) + '-' + reqTime.substr(6, 2) + ' ' + reqTime.substr(8, 2) + ':' + reqTime.substr(10, 2) + ':' + reqTime.substr(12, 2)
console.log(reqTime);
// 将时间格式转换成时间戳
dateTmp = reqTime.replace(/-/g, '/') //为了兼容IOS,需先将字符串转换为'2018/9/11 9:11:23'
// console.log(Date.parse(dateTmp));
// 将时间戳加一天减一秒
timestamp = Date.parse(dateTmp) + 1 * 24 * 60 * 60 * 1000 - 1000;
return timestamp
}
// 再次转换成时间字符串
function shijianchuo1(shijianchuo) {
var time = new Date(shijianchuo) //先将时间戳转为Date对象,然后才能使用Date的方法
var year = time.getFullYear(),
month = time.getMonth() + 1, //月份是从0开始的
day = time.getDate(),
hour = time.getHours(),
minute = time.getMinutes(),
second = time.getSeconds()
//add0()方法在后面定义,小于10的单位数字前面加0
dateFormat = year + '-' + add0(month) + '-' + add0(day) + ' ' + add0(hour) + ':' + add0(minute) + ':' + add0(second)
return dateFormat;
}
function add0(m) {
return m < 10 ? '0' + m : m
}
let endTime = shijianchuo1(addtime(timeStr));
// console.log(endTime, '1111');
let shijianchuotime = shijianchuo1(1540546832000);
// console.log(shijianchuotime, '222');
</script>
</html>
时间戳转化,时间字符串转化
最新推荐文章于 2024-07-02 00:30:00 发布