aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_reminders
diff options
context:
space:
mode:
authorChristopher Ng <chrng8@gmail.com>2024-02-15 18:44:21 -0800
committerChristopher Ng <chrng8@gmail.com>2024-03-08 03:46:55 -0800
commite309802d3210d0c3a1a65ea4671f5e7b38812846 (patch)
treeb25bbb96e2dc2a83f2fa0377042b348bad44c0db /apps/files_reminders
parent8f7829ea90ab8b716466369e6cf7d2f4a8e330d1 (diff)
downloadnextcloud-server-e309802d3210d0c3a1a65ea4671f5e7b38812846.tar.gz
nextcloud-server-e309802d3210d0c3a1a65ea4671f5e7b38812846.zip
chore(files_reminders): Drop moment in favour of native js Date API
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'apps/files_reminders')
-rw-r--r--apps/files_reminders/src/shared/utils.ts106
1 files changed, 63 insertions, 43 deletions
diff --git a/apps/files_reminders/src/shared/utils.ts b/apps/files_reminders/src/shared/utils.ts
index 07142888e51..1db42dd1392 100644
--- a/apps/files_reminders/src/shared/utils.ts
+++ b/apps/files_reminders/src/shared/utils.ts
@@ -20,7 +20,6 @@
*
*/
-import moment from '@nextcloud/moment'
import { getCanonicalLocale } from '@nextcloud/l10n'
export enum DateTimePreset {
@@ -30,58 +29,82 @@ export enum DateTimePreset {
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 = moment()
- const evening = moment()
- .startOf('day')
- .add(18, 'hour')
- const cutoff = evening
- .clone()
- .subtract(1, 'hour')
- if (now.isSameOrAfter(cutoff)) {
+ 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.toDate()
+ return evening
},
[DateTimePreset.Tomorrow]: () => {
- const day = moment()
- .add(1, 'day')
- .startOf('day')
- .add(8, 'hour')
- return day.toDate()
+ 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 = moment()
+ const today = new Date()
if (
[
5, // Friday
6, // Saturday
- 7, // Sunday
- ].includes(today.isoWeekday())
+ 0, // Sunday
+ ].includes(today.getDay())
) {
return null
}
- const saturday = moment()
- .startOf('isoWeek')
- .add(5, 'day')
- .add(8, 'hour')
- return saturday.toDate()
+ const saturday = new Date()
+ const firstWorkdayOfWeek = getFirstWorkdayOfWeek()
+ saturday.setDate(firstWorkdayOfWeek.getDate() + 5)
+ saturday.setHours(8, 0, 0, 0)
+ return saturday
},
[DateTimePreset.NextWeek]: () => {
- const today = moment()
- if (today.isoWeekday() === 7) { // Sunday
+ const today = new Date()
+ if (today.getDay() === 0) { // Sunday
return null
}
- const workday = moment()
- .startOf('isoWeek')
- .add(1, 'week')
- .add(8, 'hour')
- return workday.toDate()
+ const workday = new Date()
+ const firstWorkdayOfWeek = getFirstWorkdayOfWeek()
+ workday.setDate(firstWorkdayOfWeek.getDate() + 7)
+ workday.setHours(8, 0, 0, 0)
+ return workday
},
}
@@ -89,11 +112,10 @@ export const getDateTime = (dateTime: DateTimePreset): null | Date => {
}
export const getInitialCustomDueDate = (): Date => {
- const hour = moment().get('hour')
- const dueDate = moment()
- .startOf('day')
- .add(hour + 2, 'hour')
- return dueDate.toDate()
+ const now = new Date()
+ const dueDate = new Date()
+ dueDate.setHours(now.getHours() + 2, 0, 0, 0)
+ return dueDate
}
export const getDateString = (dueDate: Date): string => {
@@ -102,17 +124,16 @@ export const getDateString = (dueDate: Date): string => {
minute: '2-digit',
}
- const dueDateMoment = moment(dueDate)
- const today = moment()
+ const today = new Date()
- if (!dueDateMoment.isSame(today, 'date')) {
+ if (!isSameDate(dueDate, today)) {
formatOptions = {
...formatOptions,
weekday: 'short',
}
}
- if (!dueDateMoment.isSame(today, 'week')) {
+ if (!isSameWeek(dueDate, today)) {
formatOptions = {
...formatOptions,
month: 'short',
@@ -120,7 +141,7 @@ export const getDateString = (dueDate: Date): string => {
}
}
- if (!dueDateMoment.isSame(today, 'year')) {
+ if (dueDate.getFullYear() !== today.getFullYear()) {
formatOptions = {
...formatOptions,
year: 'numeric',
@@ -142,10 +163,9 @@ export const getVerboseDateString = (dueDate: Date): string => {
minute: '2-digit',
}
- const dueDateMoment = moment(dueDate)
- const today = moment()
+ const today = new Date()
- if (!dueDateMoment.isSame(today, 'year')) {
+ if (dueDate.getFullYear() !== today.getFullYear()) {
formatOptions = {
...formatOptions,
year: 'numeric',