日期
获取日期格式化字符串
javascript
// 根据传入的日期,返回格式化字符串
export function formatDate(date, format = "yyyy-MM-dd") {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const seconds = date.getSeconds().toString().padStart(2, "0");
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (match) {
switch (match) {
case "yyyy":
return year;
case "MM":
return month;
case "dd":
return day;
case "HH":
return hours;
case "mm":
return minutes;
case "ss":
return seconds;
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
获取日期对应的对象信息
javascript
// 获取当天日期
export function getCurrentDate(datea = new Date()) {
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
const date = datea;
return {
date: date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
string: formatDate(date, "yyyy-MM-dd"),
stringFull: formatDate(date, "yyyy-MM-dd HH:mm"),
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
判断日期是否是今天
javascript
export function isToday(date) {
const today = new Date();
return date.getFullYear() === today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate();
}
1
2
3
4
5
6
2
3
4
5
6
判断是否是闰年
javascript
export function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
1
2
3
2
3
获取日期在一年中的周数
javascript
// 判断当前日期是一年当中的第几周
export function getWeekOfYear(date) {
const year = date.getFullYear();
const firstDayOfYear = new Date(year, 0, 1); // 一年的第一天
const lastDayOfYear = new Date(year, 11, 31); // 一年的最后一天
const firstDayOfWeek = firstDayOfYear.getDay(); // 一年的第一天是星期几
const lastDayOfWeek = lastDayOfYear.getDay(); // 一年的最后一天是星期几
const daysInFirstWeek = 7 - firstDayOfWeek; // 一年的第一天到第一周的最后一天的天数
const daysInLastWeek = lastDayOfWeek; // 一年的最后一周的天数
const totalDays = 365 + (isLeapYear(year) ? 1 : 0); // 一年的总天数
const totalWeeks =
Math.ceil((totalDays - daysInFirstWeek - daysInLastWeek) / 7) + 2; // 一年的总周数
const firstWeekEnd = new Date(year, 0, 7 - firstDayOfWeek); // 第一周的最后一天
const lastWeekStart = new Date(year, 11, 31 - lastDayOfWeek); // 最后一周的第一天
if (date >= firstWeekEnd && date <= lastWeekStart) {
// 如果当前日期在第一周和最后一周之间
const daysInCurrentWeek =
Math.floor((date - firstWeekEnd) / (1000 * 60 * 60 * 24)) + 1; // 当前日期是第几周
return Math.ceil(daysInCurrentWeek / 7) + 1; // 返回第几周
} else if (date < firstWeekEnd) {
// 如果当前日期在第一周之前
return 1; // 返回第一周
} else {
// 如果当前日期在最后一周之后
return totalWeeks; // 返回最后一周
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
获取日期所在周的所有日期对象
javascript
export function getCurrentWeekDates(currentDate, todayDate) {
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const day = currentDate.getDate();
// Get the first day of the week (Sunday)
const firstDayOfWeek = new Date(year, month, day - currentDate.getDay());
const daysArray = [];
// Generate 7 days starting from Sunday
for (let i = 0; i < 7; i++) {
const date = new Date(firstDayOfWeek);
date.setDate(firstDayOfWeek.getDate() + i);
daysArray.push({
date: date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: date.getMonth() === month,
isSelected: false,
status: compareDateWithoutTime(date, todayDate),
string: formatDate(date, 'yyyy-MM-dd')
});
}
console.log(daysArray, 'getCurrentWeekDates')
return daysArray;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
获取周所在的所有日期对象
javascript
export function getWeekDatesByWeekNumber(year, weekNumber) {
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
// 获取该年第一天的日期
const firstDayOfYear = new Date(year, 0, 1);
// 计算第一周的第一天(周日)
const firstWeekDay = firstDayOfYear.getDay();
const firstWeekStart = new Date(firstDayOfYear);
firstWeekStart.setDate(firstDayOfYear.getDate() - firstWeekDay);
// 计算目标周的第一天
const targetWeekStart = new Date(firstWeekStart);
targetWeekStart.setDate(firstWeekStart.getDate() + (weekNumber - 1) * 7);
const daysArray = [];
// 生成7天的日期
for (let i = 0; i < 7; i++) {
const date = new Date(targetWeekStart);
date.setDate(targetWeekStart.getDate() + i);
daysArray.push({
date: date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: true,
isSelected: false,
status: compareDateWithoutTime(date, new Date()),
string: formatDate(date, 'yyyy-MM-dd')
});
}
return daysArray;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
获取日期之前/之后 n 分/时/天/周/月/年的日期对象
javascript
export function getDateBeforeOrAfter(currentDate, diff, type = "day") {
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
const date = new Date(currentDate);
if (type === "minute") {
date.setMinutes(currentDate.getMinutes() + diff);
} else if (type === "hour") {
date.setHours(currentDate.getHours() + diff);
} else if (type === "day") {
date.setDate(currentDate.getDate() + diff);
} else if (type === "week") {
date.setDate(currentDate.getDate() + diff * 7);
} else if (type === "month") {
date.setMonth(currentDate.getMonth() + diff);
} else if (type === "year") {
date.setFullYear(currentDate.getFullYear() + diff);
}
return {
date: date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
string: formatDate(date, "yyyy-MM-dd"),
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
获取日期在某日之前/后
javascript
export function compareDateWithoutTime(inputDate, todayDate = new Date()) {
const today = todayDate || new Date();
const todayDateOnly = new Date(today.getFullYear(), today.getMonth(), today.getDate());
const inputDateOnly = new Date(inputDate.getFullYear(), inputDate.getMonth(), inputDate.getDate());
if (inputDateOnly > todayDateOnly) return 'big';
if (inputDateOnly < todayDateOnly) return 'small';
return 'equal';
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
获取一个月日历矩形方格
javascript
// 获取一个月的日历(包含上个月结尾的日期和下个月开头的日期,组成一个矩形)
export function getOneMonthCanlendar(currentDate, todayDate) {
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const daysArray = [];
// 填充上个月的日期
for (let i = firstDay.getDay(); i > 0; i--) {
const date = new Date(year, month, -i + 1);
daysArray.push({
date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: false,
isSelected: false,
status: compareDateWithoutTime(date, todayDate),
string: formatDate(date, "yyyy-MM-dd"),
});
}
// 填充当前月的日期
for (let i = 1; i <= lastDay.getDate(); i++) {
const date = new Date(year, month, i);
daysArray.push({
date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: true,
isSelected: false,
status: compareDateWithoutTime(date, todayDate),
// 比当前时间小还是大:big,small,equal
string: formatDate(date, "yyyy-MM-dd"),
});
}
// 填充下个月的日期
const nextMonthDays = 7 - lastDay.getDay() - 1;
for (let i = 1; i <= nextMonthDays; i++) {
const date = new Date(year, month + 1, i);
daysArray.push({
date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: false,
isSelected: false,
status: compareDateWithoutTime(date, todayDate),
string: formatDate(date, "yyyy-MM-dd"),
});
}
return daysArray;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
获取给定日期区间内的所有日期
javascript
// 通过开始日期YYYY-MM-DD,结束日期YYYY-MM-DD,返回中间的日历数组
export function getDaysBetween(startDate, endDate) {
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
const start = new Date(startDate);
const end = new Date(endDate);
const daysArray = [];
for (let date = start; date <= end; date.setDate(date.getDate() + 1)) {
daysArray.push({
date: date,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
week: weekDays[date.getDay()],
weekIndex: date.getDay(),
weekOfYear: getWeekOfYear(date),
isCurrentMonth: date.getMonth() === start.getMonth(),
isSelected: false,
status: compareDateWithoutTime(date, new Date()),
string: formatDate(date, 'yyyy-MM-dd'),
})
}
return daysArray;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
判断两个日期是否跨天
javascript
// 判断两个日期是否跨天
export function isDateCrossDay(date1, date2) {
const year1 = date1.getFullYear();
const month1 = date1.getMonth();
const day1 = date1.getDate();
const year2 = date2.getFullYear();
const month2 = date2.getMonth();
const day2 = date2.getDate();
return year1 !== year2 || month1 !== month2 || day1 !== day2;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10