202 lines
6.7 KiB
JavaScript
202 lines
6.7 KiB
JavaScript
const yayaTime = {
|
||
/**
|
||
* @description 获取未来七天星期几,几号
|
||
*/
|
||
weekDate(){
|
||
let date = new Date()
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth()+1
|
||
let day = date.getDate()
|
||
let nth = date.getDay()//星期几
|
||
console.log(year,month,day,nth);
|
||
let xingq = ['周日','周一','周二','周三','周四','周五','周六']
|
||
console.log(`${year}-${month}-${day} ${xingq[nth]}`);
|
||
},
|
||
/**
|
||
* @description 将时间转换成几天前,几个月前,几小时前等
|
||
*/
|
||
timeago(datetime) {
|
||
if(datetime == undefined || datetime == ''){
|
||
return
|
||
}
|
||
var timePublish = new Date(datetime.replace(/-/gi, '/'));
|
||
var timeNow = new Date();
|
||
var minute = 1000 * 60;
|
||
var hour = minute * 60;
|
||
var day = hour * 24;
|
||
var month = day * 30;
|
||
var result = "0";
|
||
var diffValue = timeNow - timePublish;
|
||
var diffMonth = diffValue / month;
|
||
var diffWeek = diffValue / (7 * day);
|
||
var diffDay = diffValue / day;
|
||
var diffHour = diffValue / hour;
|
||
var diffMinute = diffValue / minute;
|
||
|
||
if (diffValue < 0) {
|
||
console.log('错误时间')
|
||
}
|
||
else if (diffDay > 1) {
|
||
var year = timePublish.getFullYear();
|
||
var month = timePublish.getMonth() + 1;
|
||
var date = timePublish.getDate();
|
||
var hours = timePublish.getHours();
|
||
var minutes = timePublish.getMinutes();
|
||
var seconds = timePublish.getSeconds();
|
||
if(month<=9){month = '0'+month}
|
||
if(date<=9){date = '0'+date}
|
||
if(hours<=9){hours = '0'+hours}
|
||
if(minutes<=9){minutes = '0'+minutes}
|
||
if(seconds<=9){seconds = '0'+seconds}
|
||
result = year+'-'+month+'-'+date;
|
||
|
||
}
|
||
else if (diffMonth > 1) {
|
||
result = parseInt(diffMonth) + "月前";
|
||
}
|
||
else if (diffWeek > 1) {
|
||
result = parseInt(diffWeek) + "周前";
|
||
}
|
||
else if (diffDay > 1 ) {
|
||
result = parseInt(diffDay) + "天前";
|
||
}
|
||
else if (diffHour > 1) {
|
||
result = parseInt(diffHour) + "小时前";
|
||
}
|
||
else if (diffMinute > 1) {
|
||
result = parseInt(diffMinute) + "分钟前";
|
||
}
|
||
else {
|
||
result = "刚刚";
|
||
}
|
||
return result;
|
||
},
|
||
/**
|
||
* @description 时间戳===>日期
|
||
*/
|
||
timestampToTime(timestamp) {
|
||
var date = timestamp.toString().length==13 ? new Date(timestamp*1) : 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() < 10 ? '0'+date.getDate() : date.getDate();
|
||
var h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours();
|
||
var m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
|
||
var s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
|
||
return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' +s;
|
||
},
|
||
/**
|
||
* @description 返回当前时间
|
||
*/
|
||
returnCurrentTime(format,type) {
|
||
let date = new Date();
|
||
let year = date.getFullYear(); // 年
|
||
let month = date.getMonth() + 1; // 月
|
||
let day = date.getDate(); // 日
|
||
let time = date.getHours(); // 时
|
||
let minu = date.getSeconds(); // 分
|
||
let second = date.getMinutes(); // 秒
|
||
|
||
let newTime = '';
|
||
switch (type){
|
||
case 0:
|
||
newTime = `${year}${format}${month < 10? '0' + month : month}${format}${day < 10 ? '0' + day : day} ${time < 10 ? '0' + time : time}:${minu < 10 ? '0' + minu : minu}`; // 2022-03-31 16:05
|
||
break;
|
||
case 1:
|
||
newTime = `${year}${format}${month < 10? '0' + month : month}${format}${day < 10 ? '0' + day : day} ${time < 10 ? '0' + time : time}:${minu < 10 ? '0' + minu : minu}:${second < 10 ? '0' + second : second}`; // 2022-03-31 16:10:07
|
||
break;
|
||
}
|
||
return newTime;
|
||
},
|
||
/**
|
||
* @description 日期===>时间戳
|
||
*/
|
||
timeToTimestamp(time){
|
||
var date = new Date(time);
|
||
var timestamp = date.getTime();//精确到毫秒
|
||
return timestamp
|
||
// var date = new Date('2014-04-23 18:55:49:123');
|
||
// 有三种方式获取
|
||
// var time1 = date.getTime();//精确到毫秒
|
||
// var time2 = date.valueOf();//精确到毫秒
|
||
// var time3 = Date.parse(date);//只能精确到秒,毫秒用000替代
|
||
// console.log(time1);//1398250549123
|
||
// console.log(time2);//1398250549123
|
||
// console.log(time3);//1398250549000
|
||
},
|
||
/**
|
||
* @description 返回时间xx天xx小时xx分钟
|
||
*/
|
||
returnTimeFormat(startTime,endTime){
|
||
let currentTimestamp = this.timeToTimestamp(endTime) - this.timeToTimestamp(startTime);
|
||
let timeStr = '';
|
||
var day = parseInt((currentTimestamp % (1000 * 60 * 60 * 24 * 12)) / (1000 * 60 * 60 * 24));
|
||
var hours = parseInt((currentTimestamp % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||
var seconds = parseInt((currentTimestamp % (1000 * 60 * 60)) / (1000 * 60));
|
||
day = day < 10 ? ('0' + day) : day;
|
||
hours = hours < 10 ? ('0' + hours) : hours;
|
||
seconds = seconds < 10 ? ('0' + seconds) : seconds;
|
||
if(day*1==0) {
|
||
if(hours*1==0) {
|
||
seconds*1==0 ? timeStr = 0 : timeStr = `${seconds}分钟`;
|
||
} else {
|
||
timeStr = `${hours}小时${seconds}分钟`;
|
||
}
|
||
} else {
|
||
timeStr = `${day}天${hours}小时${seconds}分钟`;
|
||
}
|
||
return timeStr;
|
||
},
|
||
/**
|
||
* @description 时间戳转时分秒 00 : 00 : 00
|
||
*/
|
||
formatDuring(mss) {
|
||
// let dangTime = Math.round(new Date()/1000)//获取当前时间戳
|
||
var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||
var seconds = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
|
||
var minutes = (mss % (1000 * 60)) / 1000;
|
||
hours = hours < 10 ? ('0' + hours) : hours;
|
||
seconds = seconds < 10 ? ('0' + seconds) : seconds;
|
||
minutes = minutes < 10 ? ('0' + minutes) : minutes;
|
||
return hours + ' : ' + seconds + ' : ' + minutes;
|
||
},
|
||
/**
|
||
* @description 开启倒计时
|
||
*/
|
||
dayTime(endTime,startTime=''){
|
||
let totalSecond = '';
|
||
// 本地倒计时
|
||
// if(startTime=='') totalSecond = Math.floor((new Date(endTime).getTime() - new Date().getTime())/1000);
|
||
// 解决苹果手机问题
|
||
let date = endTime;
|
||
date = endTime.replace(/-/g,'/')
|
||
// 服务器倒计时
|
||
if(startTime!='') totalSecond = Math.floor((new Date(date).getTime() - startTime)/1000);
|
||
// 总秒数
|
||
let second = totalSecond;
|
||
// 天数
|
||
let day = Math.floor(second / 3600 / 24);
|
||
let dayStr = day.toString();
|
||
if(dayStr.length == 1) dayStr = '0' + dayStr;
|
||
// 小时
|
||
let hr = Math.floor((second - day * 3600 * 24) / 3600);
|
||
let hrStr = hr.toString();
|
||
if(hrStr.length == 1) hrStr = '0' + hrStr;
|
||
// 分钟
|
||
let min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
|
||
let minStr = min.toString();
|
||
if(minStr.length == 1) minStr = '0' + minStr;
|
||
// 秒
|
||
let sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
|
||
let secStr = sec.toString();
|
||
if(secStr.length == 1) secStr = '0' + secStr;
|
||
|
||
let newTime = '';
|
||
if(dayStr==0) {
|
||
newTime = hrStr +'时'+ minStr +'分'+ secStr +'秒';
|
||
} else {
|
||
newTime = dayStr +'天'+ hrStr +'时'+ minStr +'分'+ secStr +'秒';
|
||
}
|
||
return newTime;
|
||
},
|
||
}
|
||
export default yayaTime; |