Skip to content

Commit 476754a

Browse files
committed
fix: use local timezone instead of UTC for date calculations to prevent early day rollover
1 parent 1082752 commit 476754a

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

src/js/main.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import dayjs from 'dayjs';
2-
import utc from 'dayjs/plugin/utc.js';
32
import * as bootstrap from 'bootstrap';
43
import { daysUntilNextBirthday } from './modules/days.js';
54
import { ver } from './modules/version.js';
65

7-
dayjs.extend(utc);
8-
96
// Initialize when DOM is loaded
107
document.addEventListener('DOMContentLoaded', () => {
118
// Bootstrap modals are initialized automatically via data-attributes
@@ -68,8 +65,8 @@ document.addEventListener('DOMContentLoaded', () => {
6865
const dateStart = document.getElementById('dateStart').value;
6966
const dateEnd = document.getElementById('dateEnd').value;
7067

71-
const startDate = dayjs.utc(dateStart);
72-
const endDate = dayjs.utc(dateEnd);
68+
const startDate = dayjs(dateStart);
69+
const endDate = dayjs(dateEnd);
7370

7471
let c = ver(startDate, endDate);
7572

src/js/modules/days.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import dayjs from 'dayjs';
2-
import utc from 'dayjs/plugin/utc.js';
3-
4-
dayjs.extend(utc);
52

63
/**
7-
* Returns number of days until next birthday from now (UTC)
4+
* Returns number of days until next birthday from now (local timezone)
85
* @param {Date|string|number} birthday
96
* @returns {number}
107
*/
@@ -13,17 +10,17 @@ export function daysUntilNextBirthday(birthday) {
1310
}
1411

1512
/**
16-
* Returns number of days until next birthday from a given 'now' date (UTC)
13+
* Returns number of days until next birthday from a given 'now' date (local timezone)
1714
* @param {Date|string|number} birthday
1815
* @param {Date|string|number} nowMoment
1916
* @returns {number}
2017
*/
2118
export function daysUntilNextBirthdayGivenNow(birthday, nowMoment) {
22-
const b = dayjs.utc(birthday).startOf('day');
23-
const now = dayjs.utc(nowMoment).startOf('day');
19+
const b = dayjs(birthday).startOf('day');
20+
const now = dayjs(nowMoment).startOf('day');
2421

2522
const thisYear = now.year();
26-
const birthdayThisYear = dayjs.utc(`${thisYear}-${b.format('MM')}-${b.format('DD')}`).startOf('day');
23+
const birthdayThisYear = dayjs(`${thisYear}-${b.format('MM')}-${b.format('DD')}`).startOf('day');
2724
const birthdayNextYear = birthdayThisYear.add(1, 'year').startOf('day');
2825

2926
const hadBirthdayThisYear = birthdayThisYear.isBefore(now) || birthdayThisYear.isSame(now);

src/js/modules/version.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import dayjs from 'dayjs';
2-
import utc from 'dayjs/plugin/utc.js';
3-
4-
dayjs.extend(utc);
52

63
export function ver(startDate, endDate) {
7-
const start = dayjs.utc(startDate);
8-
const end = dayjs.utc(endDate);
4+
const start = dayjs(startDate);
5+
const end = dayjs(endDate);
96

107
const years = end.diff(start, 'year');
118
const months = end.diff(start, 'month') % 12;

0 commit comments

Comments
 (0)