/**
* 获取本、上、下周开始结束时间
* @description 得到本、上、下周的起始、结束日期
* @param {Number} n 不传或0代表本周,-1代表上周,1代表下周
* @param {Number} type 0 开始时间 1 结束时间
* @author Mt.flower
*/
export function getWeek(n, type) {
// 周
let now = new Date();
let day = now.getDay(); //返回星期几的某一天;
if (!type) {
if (n == 1) {
let dayNumber = day == 0 ? 0 : 7 - day;
now.setDate(now.getDate() + dayNumber + 1);
} else if (n == -1) {
let dayNumber = day == 0 ? 6 : day - 1;
now.setDate(now.getDate() - dayNumber - 7);
} else {
let dayNumber = day == 0 ? 6 : day - 1;
now.setDate(now.getDate() - dayNumber);
}
} else {
if (n == 1) {
let dayNumber = day == 0 ? 0 : 7 - day;
now.setDate(now.getDate() + dayNumber + 1 + 6); // 在周开始的日期上+6天=周结束
} else if (n == -1) {
let dayNumber = day == 0 ? 6 : day - 1;
now.setDate(now.getDate() - dayNumber - 7 + 6);
} else {
let dayNumber = day == 0 ? 0 : 7 - day;
now.setDate(now.getDate() + dayNumber);
}
}
let date = now.getDate();
let month = now.getMonth() + 1;
let s =
now.getFullYear() +
"-" +
(month < 10 ? "0" + month : month) +
"-" +
(date < 10 ? "0" + date : date);
return s;
}
// 查看本月 type 0 开始时间 1 结束时间
export function getMonth(type) {
let date = new Date();
let month = date.getMonth() + 1;
let d = new Date(date.getFullYear(), month, 0);
let day = d.getDate();
let dayNumber = "01";
if (type) {
dayNumber = day;
}
let s =
date.getFullYear() +
"-" +
(month < 10 ? "0" + month : month) +
"-" +
dayNumber;
return s;
}
// 查看本年 type 0 开始时间 1 结束时间
export function getYear(type) {
let date = new Date();
let d = new Date(date.getFullYear(), 12, 0);
let day = d.getDate();
let month = 1;
let dayNumber = "01";
if (type) {
month = 12;
dayNumber = day;
}
let s =
date.getFullYear() +
"-" +
(month < 10 ? "0" + month : month) +
"-" +
dayNumber;
return s;
}
// 获取当前日期
export function getDate() {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let day = date.getDate();
if (day < 10) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
/**
* 获取几天之前的日期
* @param {Number} count
* @returns
*/
export function getDateAgo(count) {
let date0 = new Date();
let newDate = date0.getTime() - count * 24 * 3600 * 1000;
let date = new Date(newDate);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
return year + "-" + month + "-" + day;
}
// 指定时间转换为时间戳
function toTimeStamp(dateString) {
return new Date(dateString) - 0;
}
// 获取两个日期之间相隔的天数
export function timeDifference(date1, date2) {
// date1例如:'2022-03-05',date2例如:'2022-03-06'
const date1_timeStamp = toTimeStamp(date1);
const date2_timeStamp = toTimeStamp(date2);
let max = "",
min = "";
if (date1_timeStamp > date2_timeStamp) {
max = date1_timeStamp;
min = date2_timeStamp;
} else {
max = date2_timeStamp;
min = date1_timeStamp;
}
return (max - min) / (24 * 60 * 60 * 1000);
}
// 判断两个时间的大小
export function timeSize(date1, date2) {
const date1_timeStamp = toTimeStamp(date1);
const date2_timeStamp = toTimeStamp(date2);
if (date1_timeStamp < date2_timeStamp) {
return true;
} else {
return false;
}
}
js 封装时间时间日期处理文件
最新推荐文章于 2024-08-27 13:38:21 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)