日期转时间戳
// 日期转时间戳
function timechange(t) {
let date = new Date(t)
let time = (date.getTime())/1000
return time
}
时间戳转日期
// 时间戳转日期
function changetime(time) {
time = parseFloat('' + time);
let currentData = new Date();
let date = new Date(time);
let y = date.getFullYear();
let month = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
let d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
let m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return y + "-" + month + "-" + d;
}
将后台传过来的时间进行转换(往后+8小时)
即数据库操作时间类型为:datetime 类型
前端接收到的参数为2022-03-18T01:46:08.000+00:00 这种加了时区的格式
这个格式的数据转换成前端可以展示的数据时,得往后+8小时(如果后台传过来的时间比数据库的早八个小时)
// 将后台传过来的时间进行转换(往后+8小时)
function resolvingDate(date){
var d = new Date(date);
var month = (d.getMonth() + 1) < 10 ? '0'+(d.getMonth() + 1) : (d.getMonth() + 1);
var day = d.getDate()<10 ? '0'+d.getDate() : d.getDate();
var hours = d.getHours()<10 ? '0'+d.getHours() : d.getHours();
var min = d.getMinutes()<10 ? '0'+d.getMinutes() : d.getMinutes();
var sec = d.getSeconds()<10 ? '0'+d.getSeconds() : d.getSeconds();
times=d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec;
return times;
}
JS中国标准时间转化为年月日时分秒‘yyyy-MM-dd hh:mm:ss(vue中使用)
http://t.csdn.cn/pW042http://t.csdn.cn/pW042
捕获日期
//捕获日期
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
//捕获今天的日期
var strDate = date.getDate();
//捕获昨天的日期需要-1
//var strDate = date.getDate()-1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
//currentdate打印出来的是 2020-07-04 15:12:12
// var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
// " " + date.getHours() + seperator2 + date.getMinutes() +
// seperator2 + date.getSeconds();
//currentdate打印出来的是 2020-07-04
var currentdate2 = date.getFullYear() + seperator1 + month + seperator1 + strDate ;
console.log(currentdate2);
return currentdate2;
}
计算时间差
//前提是传进来的两个参数均为new Date()类型
getLastDateDiff(startdate,enddate) {
let difftime = enddate - this.data.startdate
var days=parseInt(difftime/(24*1000*3600));//计算相差的天数
var leave=difftime%(24*3600*1000);//计算天数后剩余的毫秒数
var h=parseInt(leave/(3600*1000));//计算小时数
//计算分钟数
var h_leave=leave%(3600*1000);
var min=parseInt(h_leave/(60*1000));
//计算秒数
var min_leave=h_leave%(60*1000);
var sec=parseInt(min_leave/1000);
// var lastDateDiff =`停留了${days}天${h}小时${min}分钟${sec}秒`;
var lastDateDiff =`${h}h${min}min${sec}s`;
return lastDateDiff;
},