Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

[Issue 56]: Date rounding bug #57

Merged
merged 1 commit into from
May 31, 2024
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
7 changes: 6 additions & 1 deletion frontend/src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ export function formatDate(dateStr: string | null) {
if (dateStr === "" || dateStr === null) {
return "";
}
const date = new Date(dateStr);

const [year, month, day] = dateStr.split("-").map(Number);

// Create a new Date object using the local time
const date = new Date(year, month - 1, day);

const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the format date function we call from SearchResultListItem - it needs to be updated to be called from the local time instead of just the UTC time (which is I think what happens when you just do new Date(dateStr)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This subtracts 1 from the month?

Copy link
Collaborator Author

@rylew1 rylew1 May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The month is just 0-based - you can open chrome console and try it by enter 3 for the month - you'll see it's April:

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andycochran I think it's ready for review now

Expand Down
30 changes: 30 additions & 0 deletions frontend/tests/components/search/SearchResultsListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,34 @@ describe("SearchResultsListItem", () => {
expect(screen.getByText("Test Opportunity")).toBeInTheDocument();
expect(screen.getByText("OPP-12345")).toBeInTheDocument();
});

getDateTestCases().forEach(({ api_date, ui_date }) => {
it(`renders formatted date ${ui_date} for API date ${api_date}`, () => {
const opportunityWithDate = {
...mockOpportunity,
summary: {
...mockOpportunity.summary,
post_date: api_date,
},
};

render(<SearchResultsListItem opportunity={opportunityWithDate} />);
expect(screen.getByText(ui_date)).toBeInTheDocument();
});
});
});

function getDateTestCases() {
return [
{ api_date: "2024-05-01", ui_date: "May 1, 2024" },
{ api_date: "2023-07-21", ui_date: "July 21, 2023" },
{ api_date: "2022-11-30", ui_date: "November 30, 2022" },
{ api_date: "2021-01-15", ui_date: "January 15, 2021" },
{ api_date: "2020-06-17", ui_date: "June 17, 2020" },
{ api_date: "2019-08-25", ui_date: "August 25, 2019" },
{ api_date: "2018-12-05", ui_date: "December 5, 2018" },
{ api_date: "2017-09-13", ui_date: "September 13, 2017" },
{ api_date: "2016-04-07", ui_date: "April 7, 2016" },
{ api_date: "2015-03-23", ui_date: "March 23, 2015" },
];
}
Loading