Skip to content

Commit

Permalink
Replace uses of MM/DD/YYYY
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdaley committed Feb 3, 2023
1 parent 39040b7 commit 601aa44
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion web/app/components/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class Sidebar extends Component {
}
}

get shortLinkBaseURL() {
get shortLinkBaseURL() {
return this.configSvc.config.short_link_base_url;
}

Expand Down
11 changes: 8 additions & 3 deletions web/app/helpers/parse-date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { helper } from "@ember/component/helper";
import parseDate from "hermes/utils/parse-date";

export default helper(([time]: [string | number | Date | undefined]) => {
return parseDate(time);
});
export default helper(
([time, monthFormat = "short"]: [
string | number | Date | undefined,
"short" | "long"
]) => {
return parseDate(time, monthFormat);
}
);
4 changes: 1 addition & 3 deletions web/app/routes/authenticated/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ export default class DocumentRoute extends Route {
}
}
if (!!doc.createdTime) {
doc.createdDate =
parseDate(doc.createdTime * 1000) +
` (${timeAgo(new Date(doc.createdTime * 1000))})`;
doc.createdDate = parseDate(doc.createdTime * 1000, "long");
}

// Build strings for created and last-modified.
Expand Down
21 changes: 12 additions & 9 deletions web/app/utils/parse-date.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/**
* Converts a valid Date/timestamp into a "MM/DD/YYYY"-formatted string.
* Converts a valid Date/timestamp into a string formatted
* as "DD MMM. YYYY" or "DD MMMM YYYY" (if monthFormat is "long").
* Returns null if the time parameter is invalid.
*/

export default function parseDate(
time?: string | number | Date
time?: string | number | Date,
monthFormat: "short" | "long" = "short"
): string | null {
if (!time) {
return null;
}
const date = new Date(time);

// `getMonth()` returns a zero-indexed month so we add 1
let month = date.getMonth() + 1;
if (isNaN(date.getTime())) {
return null;
}

let day = date.getDate();
let year = date.getFullYear();
let month = date.toLocaleString("default", { month: monthFormat });

// Nullify invalid timestamps
if (isNaN(month) || isNaN(day) || isNaN(year)) {
return null;
if (monthFormat === "short") {
month += ".";
}

return `${month}/${day}/${year}`;
return `${day} ${month} ${year}`;
}
8 changes: 7 additions & 1 deletion web/tests/integration/helpers/parse-date-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ module("Integration | Helper | parse-date", function (hooks) {
<div class="valid">
{{parse-date 628021800000}}
</div>
<div class="long">
{{parse-date 628021800000 "long"}}
</div>
<div class="invalid">
{{or (parse-date undefined) "Unknown"}}
</div>
`);

assert
.dom(".valid")
.hasText("11/25/1989", "A valid date renders correctly");
.hasText("25 Nov. 1989", "A valid date renders correctly");
assert
.dom(".long")
.hasText("25 November 1989", "The long format renders correctly");
assert.dom(".invalid").hasText("Unknown", "An invalid date returns null");
});
});
16 changes: 8 additions & 8 deletions web/tests/unit/utils/parse-date-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module("Unit | Utility | parse-date", function () {

test("it parses dates", function (assert) {
// Valid
assert.equal(parseDate(1), "12/31/1969");
assert.equal(parseDate(628021800000), "11/25/1989");
assert.equal(parseDate(628021800000 * 100), "2/14/3960");
assert.equal(parseDate("12/23/20"), "12/23/2020");
assert.equal(parseDate("1980/12/20"), "12/20/1980");
assert.equal(parseDate("November 21, 1963"), "11/21/1963");
assert.equal(parseDate("November 21, 1963 12:30"), "11/21/1963");
assert.equal(parseDate("2000-01-01T06:00:00.000-07:00"), "1/1/2000");
assert.equal(parseDate(1), "31 Dec. 1969");
assert.equal(parseDate(628021800000), "25 Nov. 1989");
assert.equal(parseDate(628021800000 * 100), "14 Feb. 3960");
assert.equal(parseDate("12/23/20", "long"), "23 December 2020");
assert.equal(parseDate("1980/12/20"), "20 Dec. 1980");
assert.equal(parseDate("November 21, 1963", "long"), "21 November 1963");
assert.equal(parseDate("November 21, 1963 12:30"), "21 Nov. 1963");
assert.equal(parseDate("2000-01-01T06:00:00.000-07:00"), "1 Jan. 2000");

// Invalid
assert.equal(parseDate(undefined), null);
Expand Down

0 comments on commit 601aa44

Please sign in to comment.