Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added datetime helper function #337

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Added datetime helper function #337

wants to merge 3 commits into from

Conversation

Monicaaawa
Copy link
Contributor

fixes #213
Wrote DateTime helper function in datetime_utils.js (under utils folder) and standardized the display format for date and time in announcement page, homepage banner, and homepage announcement banner.

@github-actions github-actions bot added announcements The announcements page homepage The homepage labels Feb 19, 2023
</time>
<b>Date:</b> Sunday, March 5, 2023
{/* <b>Date:</b> Sunday, March 5, 2023 */}
Copy link
Contributor

Choose a reason for hiding this comment

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

If you are not using this code anymore, then you should delete it to keep our code clean.

const d = new Date(timestamp);
const date = d.toLocaleDateString('en-US');
const time = d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
return `${date}, ${time}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't be returning a string with a specific format like ${date}, ${time} hardcoded in.
Consider the following use cases:

  • What if another component wants to use this format, but another component wants to display time like ${date} - ${time} instead?
  • Or what if a component doesn't care about the time, and only cares about the date?

This function should be able to handle either regardless. To fix this, we should instead return an object that maps keys to values so that each component can use the data however it wants. Like so:

return {
  date: date,
   time: time
}

And conveniently, javascript lets a simplify this code into the following that does the exact same thing:

return { date, time }

Then, in your components, you can use it like so:

const datetime = getDateTime(timestamp);
...

<p> Posted on {datetime.date}, {datetime.time} PST </p>

<time dateTime={hothStart.toISOString()} hidden>
{month} {startDay}{endDayString}, 2022
<b>Date: </b><time dateTime={hothStart.toISOString()} >
{month} {startDay}{endDayString}, 2023
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not be hardcoding the year, but also getting the year from the hothStart date.

@@ -0,0 +1,10 @@
export function getDateTime(timestamp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, there seems to be a timezone_names.js file that has functionality similar to this file, so you should move the function in that file into this file and make this file the centralized util file that handles anything 'time' related.

@github-actions github-actions bot added the schedule The workshop schedule page label Feb 20, 2023
Copy link
Contributor

@ariyin ariyin left a comment

Choose a reason for hiding this comment

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

i trust alex 👍

const time = d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
return { date, time };
} catch {
return '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Return null instead, the default of an empty object

@@ -24,6 +24,7 @@ import {

// These dates are displayed in the user's timezone
const monthFormatter = new Intl.DateTimeFormat('en-US', { month: 'short' });
const dayOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this logic into datetime_utils by creating a function called "getDayOfWeek"

@@ -0,0 +1,25 @@
function getDateTime(timestamp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In a util functions file, we should be documenting each function as much as possible so other people know how to use it. Add some comments here to describe the function. Make sure to mention what arguments it takes and what it returns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
announcements The announcements page homepage The homepage schedule The workshop schedule page
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Make a datetime_utils file
3 participants