export const calcDate = (date1, date2) => {
|
let date3 = date2 - date1;
|
|
let days = Math.floor(date3 / (24 * 3600 * 1000))
|
|
let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
|
let hours = Math.floor(leave1 / (3600 * 1000))
|
|
let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
|
let minutes = Math.floor(leave2 / (60 * 1000))
|
|
let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
|
let seconds = Math.round(date3 / 1000)
|
return {
|
leave1,
|
leave2,
|
leave3,
|
days: days,
|
hours: hours,
|
minutes: minutes,
|
seconds: seconds,
|
}
|
}
|
|
/**
|
* 日期格式化
|
*/
|
export function dateFormat(date, format) {
|
format = format || 'yyyy-MM-dd hh:mm:ss';
|
if (date !== 'Invalid Date') {
|
let o = {
|
"M+": date.getMonth() + 1, //month
|
"d+": date.getDate(), //day
|
"h+": date.getHours(), //hour
|
"m+": date.getMinutes(), //minute
|
"s+": date.getSeconds(), //second
|
"q+": Math.floor((date.getMonth() + 3) / 3), //quarter
|
"S": date.getMilliseconds() //millisecond
|
}
|
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
|
(date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
for (let k in o)
|
if (new RegExp("(" + k + ")").test(format))
|
format = format.replace(RegExp.$1,
|
RegExp.$1.length === 1 ? o[k] :
|
("00" + o[k]).substr(("" + o[k]).length));
|
return format;
|
}
|
return '';
|
|
}
|
|
export function convertTimestampToDate(timestamp, format) {
|
if (timestamp && (typeof timestamp === "string") && timestamp.indexOf("-") > -1) {
|
timestamp = timestamp.replace(/\-/g, "/")
|
}
|
let time = new Date(timestamp)
|
let formatterTime = dateFormat(time, format)
|
return formatterTime
|
}
|
|
|
//时间戳获取时间
|
function timeData(time) {
|
var date
|
if (time) {
|
date = new Date(time);
|
} else {
|
date = new Date();
|
}
|
const Y = date.getFullYear()
|
const M = date.getMonth() + 1
|
const MM = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
const D = date.getDate()
|
const DD = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
|
const h = date.getHours()
|
const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())
|
const m = date.getMinutes()
|
const mm = (date.getMinutes() < 10 ?
|
"0" + (date.getMinutes()) :
|
date.getMinutes());
|
const s = date.getSeconds()
|
const ss = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
|
return {
|
Y,
|
M,
|
MM,
|
D,
|
DD,
|
h,
|
hh,
|
m,
|
mm,
|
s,
|
ss
|
}
|
}
|
|
// 获取月天时分
|
export const TimeMinute = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp);
|
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 M + D + h + m
|
}
|
|
//获取八点-当前时间或者昨天8点到今天7点时间
|
export const getEightHourTime = (time) => {
|
const {
|
Y,
|
M,
|
MM,
|
DD,
|
D,
|
hh,
|
h,
|
mm,
|
m,
|
ss,
|
s
|
} = timeData(time)
|
const now = `${Y}-${MM}-${DD} ${hh}:00:00`
|
if (h > 8) {
|
const before = `${Y}-${MM}-${DD} 08:00:00`
|
return [before, now]
|
} else {
|
if (D - 1 > 0) {
|
const yesD = (DD - 1 < 10) ? ('0' + DD - 1) : DD - 1
|
const yesterday = `${Y}-${MM}-${yesD} 09:00:00`
|
return [yesterday, now]
|
} else {
|
if (MM - 1 > 0) {
|
const yesMM = (MM - 1 < 10) ? ('0' + MM - 1) : MM - 1
|
const yesDay = new Date(Y, yesMM, 0).getDate()
|
const lastMonDay = `${Y}-${yesMM}-${yesDay} 09:00:00`
|
return [lastMonDay, now]
|
} else {
|
const lastY = Y - 1
|
const lastYDay = `${lastY}-12-31 09:00:00`
|
return [lastYDay, now]
|
}
|
}
|
}
|
}
|
|
export const timestampToMinute = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp);
|
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;
|
}
|
|
export const timestampToTime = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp);
|
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
|
}
|
|
export const getTimeBefore = (n = 8) => {
|
const nowStamp = new Date().getTime()
|
const beforeStamp = nowStamp - (n * 60 * 60 * 1000)
|
const nowTime = timeStampTosecond(nowStamp + 1000 * 60 * 60)
|
const beforeTime = timeStampTosecond(beforeStamp)
|
return [beforeTime, nowTime]
|
}
|
|
//返回年月日时分秒
|
export const timeStampTosecond = (timestamp) => {
|
if (!timestamp) return
|
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
var date = new Date(timestamp);
|
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 + "00:00";
|
}
|