13位时间戳转年月日时分秒
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13位时间戳转年月日时分秒相关的知识,希望对你有一定的参考价值。
参考技术A //时间戳转换方法 date:时间戳数字function formatDate(date)
var date = new Date(date);
var YY = date.getFullYear() + '-';
var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return YY + MM + DD +" "+hh + mm + ss;
JS 计算两个时间戳相差年月日时分秒
参考技术A // 计算两个时间戳相差的多少年多少月多少天calculateDiffTime()
let startTime = '1629107469000' //2021-08-16 17:51
let endTime = '1976262719000' //2032-08-16 17:51
let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
let start = new Date(startTime);
let end = new Date(endTime);
let year = end.getFullYear() - start.getFullYear();
let month = end.getMonth() - start.getMonth();
let day = end.getDate() - start.getDate();
if (month < 0)
year--;
month = end.getMonth() + (12 - start.getMonth());
if (day < 0)
month--;
let index = flag.findIndex((temp) =>
return temp === start.getMonth() + 1
);
let monthLength;
if (index <= 6)
monthLength = 31;
else if (index > 6 && index <= 10)
monthLength = 30;
else
monthLength = 28;
day = end.getDate() + (monthLength - start.getDate());
this.result = `相差$year年$month月$day天`;
console.log(this.result)
# 计算两个时间戳相差的多少年多少月多少天多少小时多少分多少秒(另一种写法)
//计算两个时间戳相差的多少年多少月多少天多少小时多少分多少秒
calculateDiffTime ()
let startTime = '1629107469000' //2021-08-16 17:51
let endTime = '1976262719000' //2032-08-16 17:51
let runTime = parseInt((endTime - startTime) / 1000);
var year = Math.floor(runTime / 86400 / 365);
runTime = runTime % (86400 * 365);
var month = Math.floor(runTime / 86400 / 30);
runTime = runTime % (86400 * 30);
var day = Math.floor(runTime / 86400);
runTime = runTime % 86400;
var hour = Math.floor(runTime / 3600);
runTime = runTime % 3600;
var minute = Math.floor(runTime / 60);
runTime = runTime % 60;
var second = runTime;
console.log(`相差$year年$month月$day天$hour小时$minute分$second秒`);
return year+','+month+','+day+','+hour+','+minute+','+second;
以上是关于13位时间戳转年月日时分秒的主要内容,如果未能解决你的问题,请参考以下文章