Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/addDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function addDays(dateObj: Date, days: number, timeZone?: TimeZone | strin
const d = new Date(dateObj.getTime());

// Handle UTC calculation
if (isUTC(timeZone)) {
if (isUTC(zoneName)) {
d.setUTCDate(d.getUTCDate() + days);
return d;
}
Expand Down
2 changes: 1 addition & 1 deletion src/addMonths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function addMonths(dateObj: Date, months: number, timeZone?: TimeZone | s
const d = new Date(dateObj.getTime());

// Handle UTC calculation
if (isUTC(timeZone)) {
if (isUTC(zoneName)) {
d.setUTCMonth(d.getUTCMonth() + months);
// Adjust to last day of month if new month has fewer days
if (d.getUTCDate() < dateObj.getUTCDate()) {
Expand Down
9 changes: 8 additions & 1 deletion tests/addDays.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ describe('Local Time', () => {
});

describe('UTC Time', () => {
beforeAll(() => (process.env.TZ = 'Asia/Tokyo'));
beforeAll(() => (process.env.TZ = 'America/Los_Angeles'));

test('UTC timezone object across the daylight-saving transition', () => {
const date1 = new Date(Date.UTC(2024, 2, 9, 12));
const date2 = new Date(Date.UTC(2024, 2, 10, 12));

expect(addDays(date1, 1, { zone_name: 'UTC', gmt_offset: [0] })).toEqual(date2);
});

test('One day after 2:00 AM on January 31, 2024', () => {
const date1 = new Date(Date.UTC(2024, 0, 31, 10));
Expand Down
9 changes: 8 additions & 1 deletion tests/addMonths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ describe('Local Time', () => {
});

describe('UTC Time', () => {
beforeAll(() => (process.env.TZ = 'Asia/Tokyo'));
beforeAll(() => (process.env.TZ = 'America/Los_Angeles'));

test('UTC timezone object at the end of January', () => {
const date1 = new Date(Date.UTC(2025, 0, 31, 0, 30));
const date2 = new Date(Date.UTC(2025, 1, 28, 0, 30));

expect(addMonths(date1, 1, { zone_name: 'UTC', gmt_offset: [0] })).toEqual(date2);
});

test('One month after 2:00 AM on January 31, 2024', () => {
const date1 = new Date(Date.UTC(2024, 0, 31, 10));
Expand Down
9 changes: 8 additions & 1 deletion tests/addYears.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ describe('Local Time', () => {
});

describe('UTC Time', () => {
beforeAll(() => (process.env.TZ = 'Asia/Tokyo'));
beforeAll(() => (process.env.TZ = 'America/Los_Angeles'));

test('UTC timezone object when clamping a leap day', () => {
const date1 = new Date(Date.UTC(2024, 1, 29, 0, 30));
const date2 = new Date(Date.UTC(2025, 1, 28, 0, 30));

expect(addYears(date1, 1, { zone_name: 'UTC', gmt_offset: [0] })).toEqual(date2);
});

test('One year after 2:00 AM on January 31, 2024', () => {
const date1 = new Date(Date.UTC(2024, 0, 31, 10));
Expand Down
Loading