Skip to content

Commit bb977ab

Browse files
Migrate from moment to dayjs
1 parent fe360ae commit bb977ab

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"array-move": "^4.0.0",
4848
"browserfs": "^1.4.3",
4949
"classnames": "^2.3.2",
50+
"dayjs": "^1.11.13",
5051
"dompurify": "^3.1.6",
5152
"flexboxgrid": "^6.3.1",
5253
"flexboxgrid-helpers": "^1.1.3",
@@ -62,7 +63,6 @@
6263
"lz-string": "^1.4.4",
6364
"mdast-util-from-markdown": "^2.0.0",
6465
"mdast-util-to-hast": "^13.0.0",
65-
"moment": "^2.29.4",
6666
"normalize.css": "^8.0.1",
6767
"phaser": "^3.55.2",
6868
"query-string": "^9.0.0",

src/commons/achievement/utils/DateHelper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import moment from 'moment';
1+
import * as dayjs from 'dayjs';
22

33
const now = new Date();
44

@@ -38,12 +38,12 @@ export const timeFromExpired = (deadline?: Date) =>
3838
export const prettifyDate = (deadline?: Date) => {
3939
if (deadline === undefined) return '';
4040

41-
return moment(deadline).format('D MMMM YYYY HH:mm');
41+
return dayjs(deadline).format('D MMMM YYYY HH:mm');
4242
};
4343

4444
export const prettifyTime = (time?: Date) => {
4545
if (time === undefined) return '';
46-
return moment(time).format('HH:mm');
46+
return dayjs(time).format('HH:mm');
4747
};
4848

4949
// Converts Date to deadline countdown
@@ -54,11 +54,11 @@ export const prettifyDeadline = (deadline?: Date) => {
5454
return 'Expired';
5555
}
5656

57-
const now = moment();
57+
const now = dayjs();
5858

59-
const weeksAway = Math.ceil(moment(deadline).diff(now, 'weeks', true));
60-
const daysAway = Math.ceil(moment(deadline).diff(now, 'days', true));
61-
const hoursAway = Math.ceil(moment(deadline).diff(now, 'hours', true));
59+
const weeksAway = Math.ceil(dayjs(deadline).diff(now, 'weeks', true));
60+
const daysAway = Math.ceil(dayjs(deadline).diff(now, 'days', true));
61+
const hoursAway = Math.ceil(dayjs(deadline).diff(now, 'hours', true));
6262

6363
let prettifiedDeadline = '';
6464
if (weeksAway > 1) {

src/commons/achievement/utils/InsertFakeAchievements.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import moment from 'moment';
1+
import * as dayjs from 'dayjs';
22

33
import {
44
cardBackgroundUrl,
@@ -23,7 +23,7 @@ function insertFakeAchievements(
2323
inferencer: AchievementInferencer
2424
) {
2525
const sortedOverviews = [...assessmentOverviews].sort((overview1, overview2) =>
26-
moment(overview1.closeAt).diff(moment(overview2.closeAt))
26+
dayjs(overview1.closeAt).diff(dayjs(overview2.closeAt))
2727
);
2828
const length = assessmentOverviews.length;
2929

src/commons/utils/DateHelper.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import moment from 'moment';
1+
import * as dayjs from 'dayjs';
2+
import relativeTime from 'dayjs/plugin/relativeTime';
3+
4+
dayjs.extend(relativeTime);
25

36
/**
47
* Checks if a date is before or at the current time.
@@ -9,8 +12,8 @@ import moment from 'moment';
912
* is before the time of execution of this function.
1013
*/
1114
export const beforeNow = (dateString: string): boolean => {
12-
const date = moment(dateString);
13-
const now = moment();
15+
const date = dayjs(dateString);
16+
const now = dayjs();
1417
return date.isBefore(now);
1518
};
1619

@@ -25,25 +28,25 @@ export const beforeNow = (dateString: string): boolean => {
2528
* e.g 7th June, 20:09
2629
*/
2730
export const getPrettyDate = (dateString: string): string => {
28-
const date = moment(dateString);
31+
const date = dayjs(dateString);
2932
const prettyDate = date.format('Do MMMM, HH:mm');
3033
return prettyDate;
3134
};
3235

3336
export const getStandardDateTime = (dateString: string): string => {
34-
const date = moment(dateString);
37+
const date = dayjs(dateString);
3538
const prettyDate = date.format('MMMM Do YYYY, HH:mm');
3639
return prettyDate;
3740
};
3841

3942
export const getStandardDate = (dateString: string): string => {
40-
const date = moment(dateString);
43+
const date = dayjs(dateString);
4144
const prettyDate = date.format('MMMM Do YYYY');
4245
return prettyDate;
4346
};
4447

4548
export const getPrettyDateAfterHours = (dateString: string, hours: number): string => {
46-
const date = moment(dateString).add(hours, 'hours');
49+
const date = dayjs(dateString).add(hours, 'hours');
4750
const absolutePrettyDate = date.format('Do MMMM, HH:mm');
4851
const relativePrettyDate = date.fromNow();
4952
return `${absolutePrettyDate} (${relativePrettyDate})`;

src/pages/academy/groundControl/subcomponents/GroundControlEditCell.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Dialog, DialogBody, DialogFooter, Intent } from '@blueprintjs/core';
22
import { DateInput3 } from '@blueprintjs/datetime2';
33
import { IconNames } from '@blueprintjs/icons';
4-
import moment from 'moment';
4+
import * as dayjs from 'dayjs';
55
import React, { useCallback, useState } from 'react';
66

77
import { AssessmentOverview } from '../../../../commons/assessment/AssessmentTypes';
@@ -21,10 +21,10 @@ const EditCell: React.FC<Props> = ({ data, forOpenDate, handleAssessmentChangeDa
2121
const maxDate = new Date(2030, 11, 31);
2222

2323
const currentDateString = forOpenDate ? data.openAt : data.closeAt;
24-
const currentDate = moment(currentDateString, moment.ISO_8601, true);
24+
const currentDate = dayjs(currentDateString, undefined, true);
2525

2626
const [isDialogOpen, setDialogState] = useState(false);
27-
const [newDate, setNewDate] = useState<moment.Moment | null>(currentDate);
27+
const [newDate, setNewDate] = useState<dayjs.Dayjs | null>(currentDate);
2828

2929
const handleOpenDialog = useCallback(() => setDialogState(true), []);
3030
const handleCloseDialog = useCallback(() => setDialogState(false), []);
@@ -46,13 +46,13 @@ const EditCell: React.FC<Props> = ({ data, forOpenDate, handleAssessmentChangeDa
4646
}, [newDate, currentDate, data, handleAssessmentChangeDate, forOpenDate, handleCloseDialog]);
4747

4848
const handleParseDate = (str: string) => {
49-
const date = moment(str, dateDisplayFormat, true);
49+
const date = dayjs(str, dateDisplayFormat, true);
5050
return date.isValid() ? date.toDate() : false;
5151
};
52-
const handleFormatDate = (date: Date) => moment(date).format(dateDisplayFormat);
52+
const handleFormatDate = (date: Date) => dayjs(date).format(dateDisplayFormat);
5353

5454
const handleDateChange = React.useCallback(
55-
(selectedDate: string | null) => setNewDate(moment(selectedDate)),
55+
(selectedDate: string | null) => setNewDate(dayjs(selectedDate)),
5656
[]
5757
);
5858
const handleDateError = React.useCallback(() => {

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5589,6 +5589,11 @@ date-fns@^2.28.0:
55895589
dependencies:
55905590
"@babel/runtime" "^7.21.0"
55915591

5592+
dayjs@^1.11.13:
5593+
version "1.11.13"
5594+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.13.tgz#92430b0139055c3ebb60150aa13e860a4b5a366c"
5595+
integrity sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==
5596+
55925597
debounce@^1.2.1:
55935598
version "1.2.1"
55945599
resolved "https://registry.yarnpkg.com/debounce/-/debounce-1.2.1.tgz#38881d8f4166a5c5848020c11827b834bcb3e0a5"
@@ -9872,11 +9877,6 @@ mkdirp@~0.5.1:
98729877
dependencies:
98739878
minimist "^1.2.6"
98749879

9875-
moment@^2.29.4:
9876-
version "2.30.1"
9877-
resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae"
9878-
integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==
9879-
98809880
mqtt-packet@^6.8.0:
98819881
version "6.10.0"
98829882
resolved "https://registry.yarnpkg.com/mqtt-packet/-/mqtt-packet-6.10.0.tgz#c8b507832c4152e3e511c0efa104ae4a64cd418f"

0 commit comments

Comments
 (0)