Date.js
export const MILLISECOND_PER_SECOND = 1000;
export const MILLISECOND_PER_MINUTE = 60 * MILLISECOND_PER_SECOND;
export const MILLISECOND_PER_HOUR = 60 * MILLISECOND_PER_MINUTE;
export const MILLISECOND_PER_DAY = 24 * MILLISECOND_PER_HOUR;
export const MILLISECOND_PER_MONTH = 30 * MILLISECOND_PER_DAY;
export const MILLISECOND_PER_YEAR = 365 * MILLISECOND_PER_MONTH;
function addZero(t) {
let a = t;
if (a < 10) {
a = `0${a}`;
}
return a;
}
export function clearTime(date) {
const d = new Date(date);
if (d.toString() === "Invalid Date") {
return false;
}
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
return d;
}
export function tranTimeYM(time, split = "-") {
const d = new Date(time);
if (d.toString() === "Invalid Date") {
return false;
}
let month = d.getMonth() + 1;
month = addZero(month);
const timeYM = d.getFullYear() + split + month;
return timeYM;
}
export function tranTimeYMD(time, split = "-") {
const d = new Date(time);
if (d.toString() === "Invalid Date") {
return false;
}
let month = d.getMonth() + 1;
let date = d.getDate();
month = addZero(month);
date = addZero(date);
const timeYMD = d.getFullYear() + split + month + split + date;
return timeYMD;
}
export function tranTimeHMS(time, split = ":") {
const d = new Date(time);
if (d.toString() === "Invalid Date") {
return false;
}
const hours = addZero(d.getHours());
const minutes = addZero(d.getMinutes());
const seconds = addZero(d.getSeconds());
const timeHMS = hours + split + minutes + split + seconds;
return timeHMS;
}
export function tranTimeYMDHMS(time, split, split2) {
const d = new Date(time);
if (d.toString() === "Invalid Date") {
return false;
}
return `${tranTimeYMD(time, split)}${tranTimeHMS(time, split2)}`;
}
export function getTimeUnit(timestamps) {
if (typeof timestamps !== "number" || timestamps <= 0) {
return "0秒";
}
if (timestamps >= MILLISECOND_PER_YEAR) {
return `${parseInt(timestamps / MILLISECOND_PER_YEAR, 0)}年`;
}
if (timestamps >= MILLISECOND_PER_MONTH) {
return `${parseInt(timestamps / MILLISECOND_PER_MONTH, 0)}个月`;
}
if (timestamps >= MILLISECOND_PER_DAY) {
return `${parseInt(timestamps / MILLISECOND_PER_DAY, 0)}天`;
}
if (timestamps >= MILLISECOND_PER_HOUR) {
return `${parseInt(timestamps / MILLISECOND_PER_HOUR, 0)}小时`;
}
if (timestamps >= MILLISECOND_PER_MINUTE) {
return `${parseInt(timestamps / MILLISECOND_PER_MINUTE, 0)}分钟`;
}
if (timestamps >= MILLISECOND_PER_SECOND) {
return `${parseInt(timestamps / MILLISECOND_PER_SECOND, 0)}秒`;
}
return false;
}
export function timeToStamps(time) {
return new Date(time).getTime();
}
export function timeToyear(daytime) {
if (typeof daytime === "number") {
const newTime = new Date().getTimezoneOffset() / 60;
const time = new Date(((daytime - 1) * 24 + newTime) * 3600000 + 1);
time.setYear(time.getFullYear() - 70);
return tranTimeYMDHMS(time, "-", ":");
}
return daytime;
}
export function getPreMonth() {
let now = new Date();
const year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
if (parseInt(month, 0) < 10) {
month = `0${month}`;
}
if (parseInt(day, 0) < 10) {
day = `0${day}`;
}
now = `${year}-${month}-${day}`;
if (parseInt(month, 0) === 1) {
return `${parseInt(year, 0) - 1}-12-${day}`;
}
const preSize = new Date(year, parseInt(month, 0) - 1, 0).getDate();
if (preSize < parseInt(day, 0)) {
return `${year}-${month}-01`;
}
if (parseInt(month, 0) <= 10) {
return `${year}-0${parseInt(month, 0) - 1}-${day}`;
}
return `${year}-${parseInt(month, 0) - 1}-${day}`;
}
export function getTwoPreMonth() {
let now = new Date(getPreMonth());
const year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
if (parseInt(month, 0) < 10) {
month = `0${month}`;
}
if (parseInt(day, 0) < 10) {
day = `0${day}`;
}
now = `${year}-${month}-${day}`;
if (parseInt(month, 0) === 1) {
return `${parseInt(year, 0) - 1}-12-${day}`;
}
const preSize = new Date(year, parseInt(month, 0) - 1, 0).getDate();
if (preSize < parseInt(day, 0)) {
return `${year}-${month}-01`;
}
if (parseInt(month, 0) <= 10) {
return `${year}-0${parseInt(month, 0) - 1}-${day}`;
}
return `${year}-${parseInt(month, 0) - 1}-${day}`;
}
export function getPreDay(time, days) {
const now = new Date(time);
const sdtime3 = now.setDate(now.getDate() - days);
return sdtime3;
}
export function getPreMon(time, mon) {
const now = new Date(time);
const sdtime3 = now.setMonth(now.getMonth() - mon);
return sdtime3;
}
export function getTargetDate(date, daylength) {
let dayLength = daylength;
dayLength += 1;
const tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + dayLength);
const year = tempDate.getFullYear();
const month =
tempDate.getMonth() + 1 < 10
? `0${tempDate.getMonth() + 1}`
: tempDate.getMonth() + 1;
const day =
tempDate.getDate() < 10 ? `0${tempDate.getDate()}` : tempDate.getDate();
return `${year}-${month}-${day}`;
}
export function getDateStr(startDate, endDate, dayLength) {
const str = [];
let startdate = startDate;
str.push(startDate);
for (let i = 0; ; i + 1) {
const getDate = getTargetDate(startdate, dayLength);
startdate = getDate;
if (getDate <= endDate) {
str.push(getDate);
} else {
break;
}
}
return str;
}
export function getMonthBetween(start, end) {
const result = [];
const s = start.split("-");
const e = end.split("-");
const min = new Date();
const max = new Date();
min.setFullYear(s[0], s[1] * 1 - 1, 1);
max.setFullYear(e[0], e[1] * 1 - 1, 1);
const curr = min;
while (curr <= max) {
const month = curr.getMonth();
result.push(`${month + 1}月`);
curr.setMonth(month + 1);
}
return result;
}
export function isDuringDate(beginDateStr, endDateStr, DateTime) {
const curDate = new Date(DateTime);
const beginDate = new Date(beginDateStr);
const endDate = new Date(endDateStr);
if (curDate >= beginDate && curDate <= endDate) {
return true;
}
return false;
}
Template使用:
import { tranTimeYMDHMS } @/components/Date.js;
tranTimeYMDHMS(new Date());