Skip to content

Commit

Permalink
Merge pull request #1297 from nextstrain/dates-bce
Browse files Browse the repository at this point in the history
Correctly format BCE dates
  • Loading branch information
jameshadfield committed Mar 8, 2021
2 parents 954f2bc + 5e76c5e commit d713de0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/util/dateHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ export const prettifyDate = (unit, date) => {
const stringDate = typeof date ==="number" ? numericToCalendar(date) :
date instanceof Date ? dateToString(date) :
date;
const [year, month, day] = stringDate.split("-");
let year, month, day;
if (!stringDate.startsWith("-")) {
[year, month, day] = stringDate.split("-");
} else {
[year, month, day] = stringDate.slice(1).split("-");
year = `-${year}`;
}
switch (unit) {
case "CENTURY": // falls through
case "DECADE": // falls through
Expand Down
1 change: 1 addition & 0 deletions test/dates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ test("dates are prettified as expected", () => {
expect(prettifyDate("YEAR", "2020-01-01")).toStrictEqual("2020");
expect(prettifyDate("MONTH", "2020-01-05")).toStrictEqual("2020-Jan-05");
expect(prettifyDate("MONTH", "2020-01-01")).toStrictEqual("2020-Jan");
expect(prettifyDate("CENTURY", "-3000-01-01")).toStrictEqual("-3000"); // BCE
});

0 comments on commit d713de0

Please sign in to comment.