时间计算转换
前言
在实际开发项目的过程中通常会遇到一些时间的计算或者转换处理,以下是我关于这类计算的一部分总结。
一、将时间戳转换为年月日时分秒
代码如下:
/**
* 将时间戳转换为年月日时分秒
* @param {*} value
*/
export function formatDate(value) {
if (!value) {
return '';
}
const date = new Date(Number(value));
const y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? `0${MM}` : MM;
let d = date.getDate();
d = d < 10 ? `0${d}` : d;
let h = date.getHours();
h = h < 10 ? `0${h}` : h;
let m = date.getMinutes();
m = m < 10 ? `0${m}` : m;
let s = date.getSeconds();
s = s < 10 ? `0${s}` : s;
return `${y}-${MM}-${d} ${h}:${m}:${s}`;
}
可用moment:moment(value).format(‘YYYY-MM-DD HH:mm:ss’)
二、时间单位转换
代码如下:
/**
* 毫秒转换为时/分/秒
* @param {*} millisec 毫秒
*/
export function timeConversion(millisec) {
if (millisec >= 1000 && millisec < 60000) {
return `${(millisec / 1000).toFixed(0)}s`;
}
if (millisec >= 60000 && millisec < 3600000) {
return `${(millisec / (1000 * 60)).toFixed(0)}min`;
}
if (millisec >= 3600000) {
return `${(millisec / (1000 * 60 * 60)).toFixed(0)}h`;
}
return `${millisec}ms`;
}
三、2020-04-27T12:13:53.000+0000 格式转换为年月日时分秒
/**
* 2020-04-27T12:13:53.000+0000 格式的时间转成 YYYY-MM-DD hh:mm:ss
* @param {*} date
*/
export function rTime(date) {
const json_date = new Date(date).toJSON();
return new Date(new Date(json_date) + 8 * 3600 * 1000)
.toISOString()
.replace(/T/g, ' ')
.replace(/\.[\d]{3}Z/, '');
}
可用moment: moment(‘2020-04-27T12:13:53.000+0000’).format(‘YYYY-MM-DD HH:mm:ss’);
四、将20200101数字转换成日期格式YYYY-MM-DD
/**
* desc 将20200101数字转换成日期格式YYYY-MM-DD
* @param {string} data 20200101
* @returns {string} YYYY-MM-DD
*/
export const changeDate = (data) => {
return data.match(/(\d{4})(\d{2})(\d{2})/).filter((item, index) => index > 0).join('-');
};
可用moment: moment(‘20200101’).format(‘YYYY-MM-DD’);
五、获取当前日期的前n天日期及扩展
1.获取当前日期的前n天日期
代码如下:
/**
* 获取当前日期的前n天日期
* @days {*} 前n天
*/
const getLastDay = (days) => {
let targetDay = new Date();
let diffDays = days;
let currentDate = targetDay.getDate() //返回天(1~31)
targetDay = new Date(targetDay.setDate(currentDate - diffDays));
let year = targetDay.getFullYear();
let mon = targetDay.getMonth() + 1;
let day = targetDay.getDate();
return `${year}-${mon}-${day}`;
};
2.获取当前日期的上一工作日
以下这种方法没有计算节假日的情况。代码如下:
/**
* 获取当前日期的上一工作日
* @return {string} 上一工作日
*/
export const getLastTradingDay = () => {
let targetDay = new Date();
const currentDay = targetDay.getDay();
let diffDays = 1; // 相差的天数
// 当前为周末或周一
if (currentDay === 0 || currentDay === 1) {
diffDays = 2;
}
targetDay = targetDay.setDate(targetDay.getDate() - diffDays);
let year = new Date(targetDay).getFullYear();
let mon = new Date(targetDay).getMonth() + 1;
let day = new Date(targetDay).getDate();
return `${year}-${mon}-${day}`;
};
3.总结
根据上述两种方法,可以实现计算当前日期的前n个工作日的日期(跳过周末),获取当前日期的上一个工作日也可以由此实现(days=1)。
代码如下:
/**
* 当前日期的前n个工作日的日期(跳过周末)
* @days {*} 前n天
*/
const getLastTradingDay = (days) => {
let targetDay = new Date();
let diffDays = days;
let currentDate = targetDay.getDate() //返回天(1~31)
targetDay = new Date(targetDay.setDate(currentDate - diffDays));
let currentDay = targetDay.getDay(); // 返回周名(0-6)
if (currentDay === 0) { // 周日
targetDay = new Date(targetDay.setDate(currentDate - diffDays - 2))
} else if (currentDay === 6) { // 周六
targetDay = new Date(targetDay.setDate(currentDate - diffDays - 1))
}
let year = targetDay.getFullYear();
let mon = targetDay.getMonth() + 1;
let day = targetDay.getDate();
return `${year}-${mon}-${day}`;
};
六、获取当前日期的上一周/7个自然日
代码如下:
/**
* 获取当前日期的上一周/7个自然日
* @param {*} days 相差days天
* @return {object} 结果日期
*/
export const getDiffDate = () => {
const dt = new Date();
let diffDays = dt.getDay();
diffDays = diffDays === 0 ? 7 : diffDays;
let startDate = ''; // 目标开始日期
let endDate = ''; // 目标结束日期
let year = '';
let mon = '';
let day = '';
endDate = new Date().setDate(dt.getDate() - diffDays);
year = new Date(endDate).getFullYear();
mon = new Date(endDate).getMonth() + 1;
day = new Date(endDate).getDate();
endDate = `${year}-${mon}-${day}`;
startDate = new Date().setDate(dt.getDate() - diffDays - 6);
year = new Date(startDate).getFullYear();
mon = new Date(startDate).getMonth() + 1;
day = new Date(startDate).getDate();
startDate = `${year}-${mon}-${day}`;
return [startDate, endDate];
};