Skip to content

Commit

Permalink
Merge pull request #3 from marc-hughes/main
Browse files Browse the repository at this point in the history
Fixed bug when specifying January dates
  • Loading branch information
bbsimonbb authored Jan 18, 2024
2 parents 8378c2b + a6f02c5 commit 1507bee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
35 changes: 25 additions & 10 deletions src/DateWithoutTime.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import "jest"
import { DateWithoutTime } from "./DateWithoutTime"
import "jest";
import { DateWithoutTime } from "./DateWithoutTime";

describe("DateWithoutTime", () => {
// Date constructor
it("yyyy-MM-dd should be a midnight UTC date object", () => {
const myDate = new DateWithoutTime("1963-11-22");
expect(myDate.getDate()).toBe(22);
})
expect(myDate.getMonth()).toBe(10);
expect(myDate.getFullYear()).toBe(1963);
});

it("ISO midnight without timezone should be a midnight UTC date object", () => {
const myDate = new DateWithoutTime("1963-11-22T00:00:00");
expect(myDate.getDate()).toBe(22);
})
expect(myDate.getMonth()).toBe(10);
expect(myDate.getFullYear()).toBe(1963);
});

it("ISO string without timezone: a time close to midnight in America, after midnight in Greenwich, should take the local day",()=>{
it("ISO string without timezone: a time close to midnight in America, after midnight in Greenwich, should take the local day", () => {
const myDate = new DateWithoutTime("2022-08-09T23:00:00");
expect(myDate.getDate()).toBe(9);
})
it("Date from integers: a time close to midnight in America, after midnight in Greenwich, should take the local day",()=>{
const myDate = new DateWithoutTime(2022,7,9);
expect(myDate.getMonth()).toBe(7);
expect(myDate.getFullYear()).toBe(2022);
});
it("Date from integers: a time close to midnight in America, after midnight in Greenwich, should take the local day", () => {
const myDate = new DateWithoutTime(2022, 7, 9);
expect(myDate.getDate()).toBe(9);
})
})
expect(myDate.getMonth()).toBe(7);
expect(myDate.getFullYear()).toBe(2022);
});

it("Date from integers: a january date", () => {
const myDate = new DateWithoutTime(2024, 0, 9);
expect(myDate.getDate()).toBe(9);
expect(myDate.getMonth()).toBe(0);
expect(myDate.getFullYear()).toBe(2024);
});
});
4 changes: 2 additions & 2 deletions src/DateWithoutTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export class DateWithoutTime {
utcMidnightDateObj: Date
constructor(dateOrYearOrDaysSinceEpoch?: any, month?: number, day?: number) {
if (!isNaN(dateOrYearOrDaysSinceEpoch)) {
if (month) this.utcMidnightDateObj = new Date(Date.UTC(dateOrYearOrDaysSinceEpoch, month, day))
if ( Number.isInteger(month)) this.utcMidnightDateObj = new Date(Date.UTC(dateOrYearOrDaysSinceEpoch, month!, day))
else this.utcMidnightDateObj = new Date(dateOrYearOrDaysSinceEpoch * 86_400_000)
} else {
// if no date supplied, use Now.
Expand Down Expand Up @@ -61,4 +61,4 @@ export class DateWithoutTime {
Object.assign(options, { timeZone: "UTC" })
return this.utcMidnightDateObj.toLocaleDateString(locale, options)
}
}
}

0 comments on commit 1507bee

Please sign in to comment.