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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getCanonicalLocale } from '@nextcloud/l10n'
export enum DateTimePreset {
LaterToday = 'later-today',
Tomorrow = 'tomorrow',
ThisWeekend = 'this-weekend',
NextWeek = 'next-week',
}
const getFirstWorkdayOfWeek = () => {
const now = new Date()
now.setHours(0, 0, 0, 0)
now.setDate(now.getDate() - now.getDay() + 1)
return new Date(now)
}
const getWeek = (date: Date) => {
const dateClone = new Date(date)
dateClone.setHours(0, 0, 0, 0)
const firstDayOfYear = new Date(date.getFullYear(), 0, 1, 0, 0, 0, 0)
const daysFromFirstDay = (date.getTime() - firstDayOfYear.getTime()) / 86400000
return Math.ceil((daysFromFirstDay + firstDayOfYear.getDay() + 1) / 7)
}
const isSameWeek = (a: Date, b: Date) => {
return getWeek(a) === getWeek(b)
&& a.getFullYear() === b.getFullYear()
}
const isSameDate = (a: Date, b: Date) => {
return a.getDate() === b.getDate()
&& a.getMonth() === b.getMonth()
&& a.getFullYear() === b.getFullYear()
}
export const getDateTime = (dateTime: DateTimePreset): null | Date => {
const matchPreset: Record<DateTimePreset, () => null | Date> = {
[DateTimePreset.LaterToday]: () => {
const now = new Date()
const evening = new Date()
evening.setHours(18, 0, 0, 0)
const cutoff = new Date()
cutoff.setHours(17, 0, 0, 0)
if (now >= cutoff) {
return null
}
return evening
},
[DateTimePreset.Tomorrow]: () => {
const now = new Date()
const day = new Date()
day.setDate(now.getDate() + 1)
day.setHours(8, 0, 0, 0)
return day
},
[DateTimePreset.ThisWeekend]: () => {
const today = new Date()
if (
[
5, // Friday
6, // Saturday
0, // Sunday
].includes(today.getDay())
) {
return null
}
const saturday = new Date()
const firstWorkdayOfWeek = getFirstWorkdayOfWeek()
saturday.setDate(firstWorkdayOfWeek.getDate() + 5)
saturday.setHours(8, 0, 0, 0)
return saturday
},
[DateTimePreset.NextWeek]: () => {
const today = new Date()
if (today.getDay() === 0) { // Sunday
return null
}
const workday = new Date()
const firstWorkdayOfWeek = getFirstWorkdayOfWeek()
workday.setDate(firstWorkdayOfWeek.getDate() + 7)
workday.setHours(8, 0, 0, 0)
return workday
},
}
return matchPreset[dateTime]()
}
export const getInitialCustomDueDate = (): Date => {
const now = new Date()
const dueDate = new Date()
dueDate.setHours(now.getHours() + 2, 0, 0, 0)
return dueDate
}
export const getDateString = (dueDate: Date): string => {
let formatOptions: Intl.DateTimeFormatOptions = {
hour: 'numeric',
minute: '2-digit',
}
const today = new Date()
if (!isSameDate(dueDate, today)) {
formatOptions = {
...formatOptions,
weekday: 'short',
}
}
if (!isSameWeek(dueDate, today)) {
formatOptions = {
...formatOptions,
month: 'short',
day: 'numeric',
}
}
if (dueDate.getFullYear() !== today.getFullYear()) {
formatOptions = {
...formatOptions,
year: 'numeric',
}
}
return dueDate.toLocaleString(
getCanonicalLocale(),
formatOptions,
)
}
export const getVerboseDateString = (dueDate: Date): string => {
let formatOptions: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: '2-digit',
}
const today = new Date()
if (dueDate.getFullYear() !== today.getFullYear()) {
formatOptions = {
...formatOptions,
year: 'numeric',
}
}
return dueDate.toLocaleString(
getCanonicalLocale(),
formatOptions,
)
}
|