erp-el-element/utils/timeData.js

317 lines
8.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import moment from "moment/moment";
// 获取当月第一天
export function getFirstDayOfMonth(date) {
date.setDate(1);
return this.timeFormat(date);
}
// 获取当天时间
export function getToday(type = 'YYYY-MM-DD HH:mm:ss') {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment(moment().startOf("day").valueOf()).format(
type
);
obj.endtime = moment(moment().valueOf()).format(type);
return obj;
}
/**
*
* @returns 今日的时间戳
*/
export function getTodayTime() {
// 获取今天的开始时间戳午夜00:00:00
const startOfToday = new Date();
startOfToday.setHours(0, 0, 0, 0);
const startOfTodayTimestamp = startOfToday.getTime();
// 获取今天的结束时间戳下一天的午夜前一秒即23:59:59
const endOfToday = new Date();
endOfToday.setHours(23, 59, 59, 999); // 设置时间为23:59:59.999
const endOfTodayTimestamp = endOfToday.getTime();
return {
startOfTodayTimestamp,
endOfTodayTimestamp,
};
}
// 获取当月时间
export function getCurrMonthDays() {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment(
moment().month(moment().month()).startOf("month").valueOf()
).format("YYYY-MM-DD HH:mm:ss");
obj.endtime = moment(
moment().month(moment().month()).endOf("month").valueOf()
).format("YYYY-MM-DD HH:mm:ss");
return obj;
}
// 获取当月时间
export function getCurrMonthDay() {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment().month(moment().month()).startOf("month").valueOf()
obj.endtime = moment().endOf('day').valueOf()
return obj;
}
// 获取昨日时间
export function getYesterday(type = "YYYY-MM-DD HH:mm:ss") {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment(
moment().add(-1, "days").startOf("day").valueOf()
).format(type);
obj.endtime = moment(moment().add(-1, "days").endOf("day").valueOf()).format(
type
);
return obj;
}
// 获取近一周时间
export function getlastweek() {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment(
moment().add(-7, "days").startOf("day").valueOf()
).format("YYYY-MM-DD HH:mm:ss");
obj.endtime = moment(moment().add(-1, "days").endOf("day").valueOf()).format(
"YYYY-MM-DD HH:mm:ss"
);
return obj;
}
// 获取上个月时间
export function getLastMonthDays() {
const obj = {
starttime: "",
endtime: "",
};
obj.starttime = moment(
moment()
.month(moment().month() - 1)
.startOf("month")
.valueOf()
).format("YYYY-MM-DD HH:mm:ss");
obj.endtime = moment(
moment()
.month(moment().month() - 1)
.endOf("month")
.valueOf()
).format("YYYY-MM-DD HH:mm:ss");
return obj;
}
// 时间戳转化为 YY-mm-dd hh-mm-ss格式
export function filterTime2(time) {
var date = new Date(time);
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();
minute = minute < 10 ? "0" + minute : minute;
var s = date.getSeconds();
s = s < 10 ? "0" + s : s;
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + s;
}
// 时间戳转化为 YY-mm-dd 格式
export function filterTimeYMD(time) {
var date = new Date(time);
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;
return y + "-" + m + "-" + d;
}
// 时间戳转化为 mm-dd 格式
export function filterTimeMD(time) {
var date = new Date(time);
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
return m + "-" + d;
}
// 时间戳转化为 mm-dd 格式
export function filterTimeMDHM(time) {
var date = new Date(time);
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();
minute = minute < 10 ? "0" + minute : minute;
return m + "-" + d + " " + h + ":" + minute;
}
// 获取特定天数
export function filterTime1(time) {
var date = new Date(time);
var d = date.getDate();
d = d < 10 ? "0" + d : d;
return d;
}
// 时间戳转化为 YY-mm-dd格式
export function filterTime3(time) {
var date = new Date(time);
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;
return y + "-" + m + "-" + d;
}
// 时间戳转化为 YY-mm格式
export function filterTime4(time) {
var date = new Date(time);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
return y + "-" + m;
}
// 根据每月的时间戳获取最后一天的时间戳
export function filterTime5(time) {
const now = new Date(time); // 当前日期
// console.log(now)
const nowMonth = now.getMonth(); // 当前月
const nowYear = now.getFullYear(); // 当前年
// this.tmieformat()
// console.log(nowMonth + 1)
const monthEndDate = new Date(nowYear, nowMonth + 1, 0, 23, 59, 59);
const c = Date.parse(monthEndDate);
return c;
}
// 返回小时
export function filterTimeHH(time) {
var date = new Date(time);
var h = date.getHours();
h = h < 10 ? "0" + h : h;
return h;
}
// 返回月天小时
export function filterTimeMDHS(time) {
var date = new Date(time);
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;
return m + "月" + d + "日" + h;
}
// 复杂数组对象去重
export function uniqueArray(array) {
return array.reduce((accumulator, currentValue) => {
const stringified = JSON.stringify(currentValue);
if (!accumulator.some((item) => JSON.stringify(item) === stringified)) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
}
// 判断当月有多少天
export function getDaysInCurrentMonth() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const nextMonthFirstDay = new Date(year, month, 1);
const lastDay = new Date(nextMonthFirstDay - 1);
return lastDay.getDate();
}
export function getnewdate(dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
if (dataStr) {
var date = new Date(dataStr / 1);
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();
minute = minute < 10 ? "0" + minute : minute;
var s = date.getSeconds();
s = s < 10 ? "0" + s : s;
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + s;
} else {
return "";
}
}
/**
*
* @returns 当前日期,格式为"yyyy-mm-dd"
*/
export function getCurrentDate() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, "0");
var mm = String(today.getMonth() + 1).padStart(2, "0"); // 注意月份是从0开始的所以需要+1
var yyyy = today.getFullYear();
return yyyy + "-" + mm + "-" + dd;
}
/**
* @param {String} time 'yyyy-mm-dd'
* @returns 'yyyy-mm-dd'转为时间戳
*/
export function dateToTimestamp(time) {
if (time) {
var date = new Date(time);
return date.getTime();
}
}
/**
* @param {*} dateStr
* @returns '传入日期的最后一秒时间戳'
*/
export function getLastSecondTimestamp(dateStr) {
// 创建一个Date对象
var date = new Date(dateStr);
// 设置时间为当天的最后一秒
date.setHours(23, 59, 59);
// 返回时间戳
return date.getTime();
}
/**
* @returns '上个月的时间戳'
*/
export function getLastMounthTime() {
// 获取当前日期
const now = new Date();
// 设置日期为上个月的第一天
const startOfPreviousMonth = new Date(
now.getFullYear(),
now.getMonth() - 1,
1
);
// 设置日期为上个月的最后一天
const endOfPreviousMonth = new Date(now.getFullYear(), now.getMonth(), 0);
// 设置时间为当天的最后一秒
endOfPreviousMonth.setHours(23, 59, 59, 999);
// 获取时间戳
const startOfLastMonth = startOfPreviousMonth.getTime();
const endOfLastMonth = endOfPreviousMonth.getTime();
return {
startOfLastMonth,
endOfLastMonth,
};
}