174 lines
4.2 KiB
JavaScript
174 lines
4.2 KiB
JavaScript
|
/**
|
|||
|
* @Author YuanWei
|
|||
|
* @Date 2021/01/13 17:39:28
|
|||
|
* @FileName date.js
|
|||
|
* @Description: 时间工具类
|
|||
|
*/
|
|||
|
import dayjs from 'dayjs';
|
|||
|
import 'dayjs/locale/zh-cn';
|
|||
|
|
|||
|
// import localeData from 'dayjs/plugin/localeData'
|
|||
|
import isSameOrAfterPlugin from 'dayjs/plugin/isSameOrAfter';
|
|||
|
import isSameOrBeforePlugin from 'dayjs/plugin/isSameOrBefore';
|
|||
|
|
|||
|
dayjs.locale('zh-cn');
|
|||
|
// dayjs.extend(localeData)
|
|||
|
dayjs.extend(isSameOrAfterPlugin);
|
|||
|
dayjs.extend(isSameOrBeforePlugin);
|
|||
|
|
|||
|
const dateFormat = {
|
|||
|
DATE_PATTERN_FLAT: 'YYYY-MM-DD HH:mm:ss',
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 检查时间字段,字符串则转换为日期对象
|
|||
|
* @param param
|
|||
|
* @returns {Date}
|
|||
|
*/
|
|||
|
const transformParam = function (param) {
|
|||
|
return typeof param === 'string' ? new Date(param) : param;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当天开始时间
|
|||
|
* @returns {Date}
|
|||
|
*/
|
|||
|
export const getToDayDate = function () {
|
|||
|
return dayjs().startOf('day').toDate();
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 获取指定日期的开始日期部分时间
|
|||
|
* @param date
|
|||
|
*/
|
|||
|
export const getDayDate = function (date) {
|
|||
|
return dayjs(transformParam(date)).startOf('day').toDate();
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 格式化时间
|
|||
|
* @param date 需要格式化的时间(string|Date)
|
|||
|
* @param format 时间格式
|
|||
|
* @returns {string} 返回字符串时间
|
|||
|
*/
|
|||
|
export function formatterDate(date, format = dateFormat.DATE_PATTERN_FLAT) {
|
|||
|
return dayjs(transformParam(date)).format(format);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 格式化秒
|
|||
|
* @param second
|
|||
|
* @param format
|
|||
|
* @returns {string}
|
|||
|
*/
|
|||
|
export function formatterSecond(second, format = dateFormat.DATE_PATTERN_FLAT) {
|
|||
|
return dayjs(new Date(second * 1000)).format(format);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 格式化毫秒
|
|||
|
* @param millisecond
|
|||
|
* @param format
|
|||
|
* @returns {string}
|
|||
|
*/
|
|||
|
export function formatterMillisecond(millisecond, format = dateFormat.DATE_PATTERN_FLAT) {
|
|||
|
return dayjs(new Date(millisecond)).format(format);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 格式化秒或毫秒
|
|||
|
* @param time
|
|||
|
* @param format
|
|||
|
*/
|
|||
|
export function formatterSecondOrMillisecond(time, format = dateFormat.DATE_PATTERN_FLAT) {
|
|||
|
let length = String(time).length;
|
|||
|
let temp;
|
|||
|
try {
|
|||
|
temp = parseInt(time);
|
|||
|
} catch (e) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
//判断时间是否为负数(小于1970)
|
|||
|
if (temp > 0) {
|
|||
|
if (length === 13) {
|
|||
|
//毫秒级
|
|||
|
return formatterMillisecond(temp, format);
|
|||
|
} else if (length === 10) {
|
|||
|
//秒级
|
|||
|
return formatterSecond(temp, format);
|
|||
|
} else {
|
|||
|
return null;
|
|||
|
}
|
|||
|
} else {
|
|||
|
//因为多了负号所以长度加1
|
|||
|
if (length === 14) {
|
|||
|
//毫秒级
|
|||
|
return formatterMillisecond(temp, format);
|
|||
|
} else if (length === 11) {
|
|||
|
//秒级
|
|||
|
return formatterSecond(temp, format);
|
|||
|
} else {
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// throw new Error('time not is second or millisecond')
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 判断时间是否是今天
|
|||
|
* @param date
|
|||
|
* @returns {boolean}
|
|||
|
*/
|
|||
|
export function isToDay(date) {
|
|||
|
return dayjs(new Date()).isSame(transformParam(date), 'day');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 是否在今天之前
|
|||
|
* @param date
|
|||
|
*/
|
|||
|
export function isToDayOrBefore(date) {
|
|||
|
return dayjs(new Date()).isSameOrBefore(transformParam(date), 'day');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 是否在今天之后
|
|||
|
* @param date
|
|||
|
*/
|
|||
|
export function isToDayOrAfter(date) {
|
|||
|
return dayjs(new Date()).isSameOrAfter(transformParam(date), 'day');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 解析日期,返回一个date对象
|
|||
|
* @param date
|
|||
|
* @param format
|
|||
|
* @returns {Date}
|
|||
|
*/
|
|||
|
export function parseDate(date, format = dateFormat.DATE_PATTERN_FLAT) {
|
|||
|
return dayjs(date, format).toDate();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取指定日期所在的周一时间
|
|||
|
* @param date
|
|||
|
* @returns {Date}
|
|||
|
*/
|
|||
|
export function getWeekMonday(date) {
|
|||
|
return dayjs(transformParam(date)).startOf('week').toDate();
|
|||
|
}
|
|||
|
/*毫秒时间戳转换为 “xxxx年xx月xx日” 格式的代码:*/
|
|||
|
export function formatMillisecondsToChineseDateString(milliseconds) {
|
|||
|
// 创建一个新的Date对象,使用传入的毫秒时间戳
|
|||
|
const date = new Date(milliseconds);
|
|||
|
|
|||
|
// 获取年、月、日
|
|||
|
const year = date.getFullYear();
|
|||
|
const month = date.getMonth() + 1; // getMonth() 返回的月份是从0开始的,所以需要加1
|
|||
|
const day = date.getDate();
|
|||
|
|
|||
|
// 返回格式化的日期字符串
|
|||
|
return `${year}年${month}月${day}日`;
|
|||
|
}
|