Skip to content

Commit

Permalink
fix: correct handling of day in date
Browse files Browse the repository at this point in the history
  • Loading branch information
Askestad authored and Askestad committed Nov 7, 2019
1 parent 39794c0 commit af95839
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/pad-with-leading-zero.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { padWithLeadingZero } from "./pad-with-leading-zero";

describe("Pad with leading zero", () => {
it("should pad a single digit with a leading zero", () => {
expect(padWithLeadingZero(4)).toBe("04");
});

it("should not pad a double digit number", () => {
expect(padWithLeadingZero(14)).toBe("14");
});
});
4 changes: 4 additions & 0 deletions src/pad-with-leading-zero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function padWithLeadingZero(n: number) {
const retval = `0${n}`;
return retval.slice(-2);
}
4 changes: 3 additions & 1 deletion src/parse-date-of-birth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { padWithLeadingZero } from "./pad-with-leading-zero";

export function parseDateOfBirth(dateOfBirth: string): Date {
const year = +dateOfBirth.substr(0, 4);
const month = dateOfBirth.substr(4, 2);
Expand All @@ -6,5 +8,5 @@ export function parseDateOfBirth(dateOfBirth: string): Date {
if (day >= 32) {
day -= 60;
}
return new Date(`${year}-${month}-${day}`);
return new Date(`${year}-${month}-${padWithLeadingZero(day)}`);
}

0 comments on commit af95839

Please sign in to comment.