Skip to content

Commit 076fd7a

Browse files
Migrate from moment to dayjs (#3053)
1 parent 32e2cff commit 076fd7a

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-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 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 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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import moment from 'moment';
1+
import dayjs from 'dayjs';
2+
import advancedFormat from 'dayjs/plugin/advancedFormat';
3+
import relativeTime from 'dayjs/plugin/relativeTime';
4+
5+
dayjs.extend(advancedFormat);
6+
dayjs.extend(relativeTime);
27

38
/**
49
* Checks if a date is before or at the current time.
@@ -9,8 +14,8 @@ import moment from 'moment';
914
* is before the time of execution of this function.
1015
*/
1116
export const beforeNow = (dateString: string): boolean => {
12-
const date = moment(dateString);
13-
const now = moment();
17+
const date = dayjs(dateString);
18+
const now = dayjs();
1419
return date.isBefore(now);
1520
};
1621

@@ -25,25 +30,25 @@ export const beforeNow = (dateString: string): boolean => {
2530
* e.g 7th June, 20:09
2631
*/
2732
export const getPrettyDate = (dateString: string): string => {
28-
const date = moment(dateString);
33+
const date = dayjs(dateString);
2934
const prettyDate = date.format('Do MMMM, HH:mm');
3035
return prettyDate;
3136
};
3237

3338
export const getStandardDateTime = (dateString: string): string => {
34-
const date = moment(dateString);
39+
const date = dayjs(dateString);
3540
const prettyDate = date.format('MMMM Do YYYY, HH:mm');
3641
return prettyDate;
3742
};
3843

3944
export const getStandardDate = (dateString: string): string => {
40-
const date = moment(dateString);
45+
const date = dayjs(dateString);
4146
const prettyDate = date.format('MMMM Do YYYY');
4247
return prettyDate;
4348
};
4449

4550
export const getPrettyDateAfterHours = (dateString: string, hours: number): string => {
46-
const date = moment(dateString).add(hours, 'hours');
51+
const date = dayjs(dateString).add(hours, 'hours');
4752
const absolutePrettyDate = date.format('Do MMMM, HH:mm');
4853
const relativePrettyDate = date.fromNow();
4954
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 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
@@ -5596,6 +5596,11 @@ date-fns@^2.28.0:
55965596
dependencies:
55975597
"@babel/runtime" "^7.21.0"
55985598

5599+
dayjs@^1.11.13:
5600+
version "1.11.13"
5601+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.13.tgz#92430b0139055c3ebb60150aa13e860a4b5a366c"
5602+
integrity sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==
5603+
55995604
debounce@^1.2.1:
56005605
version "1.2.1"
56015606
resolved "https://registry.yarnpkg.com/debounce/-/debounce-1.2.1.tgz#38881d8f4166a5c5848020c11827b834bcb3e0a5"
@@ -9879,11 +9884,6 @@ mkdirp@~0.5.1:
98799884
dependencies:
98809885
minimist "^1.2.6"
98819886

9882-
moment@^2.29.4:
9883-
version "2.30.1"
9884-
resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae"
9885-
integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==
9886-
98879887
mqtt-packet@^6.8.0:
98889888
version "6.10.0"
98899889
resolved "https://registry.yarnpkg.com/mqtt-packet/-/mqtt-packet-6.10.0.tgz#c8b507832c4152e3e511c0efa104ae4a64cd418f"

0 commit comments

Comments
 (0)