TypeScript日期方法封装
1. 获取当前日期,格式YYYY-MM-DD
2. 获取当前时间,格式YYYY-MM-DD HH:mm:ss
3. 返回一年的总天数
4. 返回日期是当年第多少天
5. 返回时间范围的所有周末
6. 返回该日期的周末日期
7. 返回时间范围的各个月份的总天数
8. 返回日期范围内所有周末,一级前一个与后一个
9. 返回以四天为一段的日期数组
10. 返回时间范围的每一天日期对象
11. 返回日期所属的周末日期
12. 返回时间字符串
13. 返回星期字符串
export function getNowFormatDay(nowDate: Date | null = null, char: string = "-") {
if (nowDate == null) {
nowDate = new Date();
}
let day = nowDate.getDate();
let month = nowDate.getMonth() + 1;
let year = nowDate.getFullYear();
return year + char + completeDate(month) + char + completeDate(day);
}
export function getNowFormatTime(nowDate: Date | null = null, char: string = "-", colon: string = ":", bool: boolean = false) {
if (nowDate == null) {
nowDate = new Date();
}
let h = nowDate.getHours();
let m = nowDate.getMinutes();
let s = nowDate.getSeconds();
if (bool) {
return [getNowFormatDay(nowDate, char), completeDate(h) + colon + completeDate(m) + colon + completeDate(s)];
}
return getNowFormatDay(nowDate, char) + " " + completeDate(h) + colon + completeDate(m) + colon + completeDate(s);
}
function completeDate(value: number) {
return value < 10 ? "0" + value : value;
}
export const allDays = (year: number) => {
let leapYear = false, sum_day = 0, month_arr = [4, 6, 9, 11];
if (year % 100 === 0) {
leapYear = year % 400 === 0
} else {
leapYear = year % 4 === 0
}
for (let i = 1; i < 13; i++) {
if (i === 2) {
sum_day += leapYear ? 29 : 28
} else if (month_arr.includes(i)) {
sum_day += 30
} else {
sum_day += 31
}
}
return sum_day
}
export function getDays(s: `${number}-${number}-${number}`) {
const arr: any = s.split('-')
const d1 = new Date(arr[0], 0, 0).getTime()
const d2 = new Date(arr[0], arr[1] - 1, arr[2]).getTime()
const d = Math.floor((d2 - d1) / (1000 * 60 * 60 * 24))
return d
}
export function getSundayDates(startDate: Date, endDate: Date) {
const oneDay = 24 * 60 * 60 * 1000;
const result: Date[] = [];
let currentSunday = getNextSunday(startDate);
while (currentSunday <= endDate) {
result.push(currentSunday);
currentSunday = new Date(currentSunday.getTime() + 7 * oneDay);
}
let weekend = getWeekends(startDate, endDate)
result.unshift(weekend[0].previousWeekend)
return result;
}
function getNextSunday(date: Date) {
const dayOfWeek = date.getDay();
const daysUntilNextSunday = 7 - dayOfWeek;
const nextSunday = new Date(date.getTime() + daysUntilNextSunday * 24 * 60 * 60 * 1000);
return nextSunday;
}
export function getMonthDays(startDate: Date, endDate: Date) {
const months: {
year: number,
month: number,
days: number
}[] = [];
while (startDate <= endDate) {
const year = startDate.getFullYear();
const month = startDate.getMonth();
const daysInMonth = new Date(year, month + 1, 0).getDate();
months.push({
year: year,
month: month + 1,
days: daysInMonth
});
startDate.setMonth(month + 1);
}
return months;
}
interface IgetWeekends {
date: Date,
previousWeekend: Date,
nextWeekend: Date
}
function getWeekends(startDate: any, endDate: any): Array<IgetWeekends> {
const weekends: IgetWeekends[] = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
const dayOfWeek = currentDate.getDay();
if (dayOfWeek === 0) {
weekends.push({
date: new Date(currentDate),
previousWeekend: getPreviousWeekend(currentDate),
nextWeekend: getNextWeekend(currentDate)
});
}
currentDate.setDate(currentDate.getDate() + 1);
}
return weekends;
}
function getPreviousWeekend(date: Date) {
const previousDate = new Date(date);
previousDate.setDate(previousDate.getDate() - 1);
while (previousDate.getDay() !== 0) {
previousDate.setDate(previousDate.getDate() - 1);
}
return new Date(previousDate);
}
function getNextWeekend(date: any) {
const nextDate = new Date(date);
nextDate.setDate(nextDate.getDate() + 1);
while (nextDate.getDay() !== 0) {
nextDate.setDate(nextDate.getDate() + 1);
}
return new Date(nextDate);
}
export function getDates(startDate: Date, endDate: Date) {
const dates: Date[] = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 4);
}
return dates;
}
export function getDate(startDate: Date, endDate: Date) {
const dates: Date[] = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
}
export function recentlyWeek(currentDate: Date): Date {
while (currentDate.getDay() < 6) {
currentDate = new Date(currentDate);
currentDate.setDate(currentDate.getDate() + 1);
}
return currentDate;
}
export let DateTime = (date: Date, fame = "/") => {
return date.getFullYear() + fame + (date.getMonth() + 1) + fame + date.getDate()
}
export let textWeek=(date: Date=new Date())=>{
switch (date.getDay()) {
case 0:
return "日";
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
default:
return "六";
}
}