/**
* 判断是否是日期格式
* @param { Date } date 传入的日期: new Date() 或者 "2002-03-22" 或者 "2002/03/22" 等
*/
const isTypeDateFormat = (date) => !isNaN(Date.parse(date)) || date instanceof Date;
/**
* 日期时间格式转化
* @param date 时间
* @param fmt 转化格式 yyyy-MM-dd hh:mm:ss 不传转成:yyyy-MM-dd
* @returns 格式时间字符
*/
const formatDate = (date, fmt = 'yyyy-MM-dd') => {
if (date === null) return;
if (typeof (date) == 'string' || typeof (date) == 'number') date = new Date(date);
if (/(y+)/.test(fmt)) {
const opt = {
'y+': date.getFullYear().toString(), // 年
'M+': (date.getMonth() + 1).toString(), // 月
'd+': date.getDate().toString(), // 日
'h+': date.getHours().toString(), // 时
'm+': date.getMinutes().toString(), // 分
's+': date.getSeconds().toString() // 秒
};
for (const k in opt) {
const ret = new RegExp('(' + k + ')').exec(fmt)
if (ret) {
if (/(y+)/.test(k)) {
fmt = fmt.replace(ret[1], opt[k].substring(4 - ret[1].length))
} else {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
};
};
};
};
return fmt;
};
/**
* 计算两个日期相隔多少天,不限制先后顺序
* @param {Date} Date_start 开始时间
* @param {Date} Date_end 结束时间
* @returns 相隔天数
* 可传一个参数,第二个参数不传为当前日期
*/
const beApartDays = (Date_start, Date_end = new Date()) => {
// 时间格式化
let date1 = new Date(Date_start);
let date2 = new Date(Date_end);
date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
//目标时间减去当前时间
const diff = date1.getTime() - date2.getTime();
//计算当前时间与结束时间之间相差天数
return Math.abs(diff) / (24 * 60 * 60 * 1000);
};
/**
* 计算两个月之间的月份,返回包含他们自身所有的月份
* @param { array } date 月份日期数组: [new Date(), new Date()] 或者 ["2023-02", "2023-06"]
* @returns { array } 返回来两个月份之间所有的 月份 比如 ["2023-02", "2023-06"] 返回 ["2023-02", "2023-04", "2023-05", "2023-06"]
*/
const expandMonthsArray = (date) => {
const startDate = new Date(date[0]);
const endDate = new Date(date[1]);
const result = [];
while (startDate <= endDate) {
result.push(formatDate(startDate, "yyyy-MM"));
startDate.setMonth(startDate.getMonth() + 1);
};
return result;
};
/**
* 日期天数计算
* @param { Date } start: 开始时间: start = new Date() 或者 "2002-03-28"
* @param { Date } end: 结束时间:end = new Date() 或者 "2002-04-28"
* @param { boolean } type: 调用传 true,使用递归确认什么节点为第一次
* @returns 格式时间字符
*/
let str = "";
const dateDays = (start, end, type) => {
let origin = new Date(start).getTime();
let finish = new Date(end).getTime();
if (type) str = "";
if (origin == finish) {
str += formatDate(new Date(origin), "yyyy-MM-dd") + ",";
} else {
if (origin < finish) {
str += formatDate(new Date(origin), "yyyy-MM-dd") + ",";
dateDays(formatDate(new Date(origin + 60 * 60 * 24 * 1000), "yyyy-MM-dd"), end);
};
};
return str.slice(0, str.length - 1);
};
// 调用方式
dateDays("开始时间", "结束时间", "布尔值true");
/**
* 日期天数计算带星期
* @param { Date } start: 开始时间: start = new Date() 或者 "2002-03-28"
* @param { Date } end: 结束时间:end = new Date() 或者 "2002-04-28"
* @param { boolean } type: 调用传 true,使用递归确认什么节点为第一次
* @returns 格式时间带星期的字符
*/
const dateDayWeek = (start, end, type) => {
let val = "";
const weekList = ['日', '一', '二', '三', '四', '五', '六'];
let origin = new Date(start).getTime();
let finish = new Date(end).getTime();
if (type) val = "";
if (origin == finish) {
val += formatDate(new Date(origin), "yyyy-MM-dd") + " 星期" + weekList[new Date(origin).getDay()] + ",";
} else {
if (origin < finish) {
val += formatDate(new Date(origin), "yyyy-MM-dd") + " 星期" + weekList[new Date(origin).getDay()] + ",";
dateDayWeek(new Date(origin + 60 * 60 * 24 * 1000), end);
};
};
return val.slice(0, val.length - 1);
};
/**
* 传入一个日期,返回该日期所在月份的天数
* @param date 传入一个相对日期或者字符串日期
* @returns 当前日期月份的天数
*/
const getDaysInMonth = (date) => {
// 使用 Date 对象的构造函数创建一个新的日期对象
const newDate = new Date(date);
// 获取该日期所在月份的下一个月份
const nextMonth = newDate.getMonth() + 1;
// 设置日期对象的月份为下一个月份的第 0 天
newDate.setMonth(nextMonth, 0);
// 返回该日期所在月份的天数
return newDate.getDate();
};
/**
* 近几天,返回传入值的天数
* @param { Date } date 日期时间:new Date() 或者 字符串日期 2002-03-04
* @param { number } day 传入几,就是几天,负数,今天之前的天数,正数今天以后的天数
* @returns 第几天的日期 格式:2022-02-04
*/
const inRecentDays = (date = new Date(), day = -7) => formatDate(new Date(new Date(date).getTime() + (86400000 * day)));
/**
* 传入日期获取这个月份的开始结束日期
* @param { Date } date 传入的日期
* @returns 返回这个月份的开始日期和结束日期的数组
*/
const dateStartEndDate = (date = new Date()) => {
// 获取当前时间
const now = new Date(date);
// 获取当前时间的月份
const currentMonth = now.getMonth();
// 获取当前月份的开始日期(1 号)
const currentMonthStart = formatDate(new Date(now.getFullYear(), currentMonth, 1));
// 获取当前月份的结束日期(最后一天)
const currentMonthEnd = formatDate(new Date(now.getFullYear(), currentMonth + 1, 0));
return [currentMonthStart, currentMonthEnd];
};
/**
* 通过一个日期返回这个日期所在周的开始日期和结束日期
* @param { Date } date 传入日期,或字符串日期
* @param { string } start 开始日期的字段
* @param { string } end 结束日期的字段
* @returns 返回日期所在周的开始结束日期
*/
const getWeekDates = (date = new Date(), start, end) => {
let weekField = {
};
const weekStart = new Date(date);
weekStart.setDate(new Date(date).getDate() - new Date(date).getDay() + 1);
const weekEnd = new Date(date);
weekEnd.setDate(new Date(date).getDate() - new Date(date).getDay() + 7);
if (start) weekField[start] = formatDate(weekStart);
else weekField.startTime = formatDate(weekStart);
if (end) weekField[end] = formatDate(weekEnd);
else weekField.endTime = formatDate(weekEnd);
return weekField;
};
/**
* 传入一个日期获取当前日期所在周的所有日期,不传默认当天
* @param { string | date } val 日期
* @returns 返回所在天的本周所有日期数据
*/
const circumferenceDay = (val = new Date()) => {
// 判断当前传入时间是否是 string 日期或者 是不是 Date 日期
if (isTypeDateFormat(val)) {
let date = new Date(val);
let day = date.getDay() || 7;
let start = new Date(date.getTime() - ((day - 1) * 86400000));
let arr = new Array();
for (let i = 0; i < 7; i++) {
arr.push(formatDate(new Date(start.getTime() + (i * 86400000))));
};
return arr;
} else {
throw Error("获取当前日期所在的周的函数传入的日期格式有误");
};
};
【JS】列举一些日期时间处理函数,总结前端日期时间功能,持续更新本文章
猜你喜欢
转载自blog.csdn.net/weixin_44244230/article/details/132083794
今日推荐
周排行