发布时间:2024-07-18 14:01
const difference = Date1-Date2; //毫秒差
const day = Math.floor((Date1-Date2) / (24 * 60 * 60 * 1000)); //天数
const keep = (Date1-Date2) % (24 * 60 * 60 * 1000); //天数剩余的毫秒数
const hour = Math.floor(keep / (60 * 60 * 1000)); //小时数
new Date("2021-09-06 14:32:08:123").getTime() //1630909928123
new Date("2021-09-06 14:32:08:123").valueOf() //1630909928123
Date.parse(new Date("2021-07-26 18:55:49:123")) //1630909928000
var time = new Date(timestamp);
var y = time.getFullYear();//getFullYear方法以四位数字返回年份
var M = time.getMonth() + 1;// getMonth方法从 Date 对象返回月份 (0 ~ 11),返回结果需要手动加一var d = time.getDate();// getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
var h = time.getHours();// getHours方法返回 Date 对象的小时 (0 ~ 23)
var m = time.getMinutes();// getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
var s = time.getSeconds();// getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
const now = new Date().getTime();//获取当前时间并转换为毫秒
const nowTime = new Date().toLocaleString(); //获取当前时间2022/8/15 2:00:00(String)
parseInt(nowTime) - parseInt(oldTime) //时间戳之间比较时需要转换为int类型进行比较
<view>
{{timestampToTime(time))}}
</view>
methods: {
// 时间戳转换为日期
timestampToTime (time) {
var date = new Date(time)
var len = time.toString().length;
if(len < 13) {
var sub = 13 - len;
sub = Math.pow(10,sub);
date = new Date(time * sub);
}
// var y = date.getFullYear() + '年';
var M = date.getMonth() + 1;
M = (M < 10 ? '0' + M : M) + '月';
var d = date.getDate();
d = (d < 10 ? '0' + d : d) + '日';
var h = date.getHours();
h = (h < 10 ? '0' + h : h) + ':';
var m = date.getMinutes();
m = (m < 10 ? '0' + m : m);
return M + d + h + m;
}
}
或者
var date = new Date(startTime)
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? ('0' + m) : m
var d = date.getDate()
d = d < 10 ? ('0' + d) : d
var h = date.getHours()
h = h < 10 ? ('0' + h) : h
var minute = date.getMinutes()
var second = date.getSeconds()
minute = minute < 10 ? ('0' + minute) : minute
second = second < 10 ? ('0' + second) : second
return ....