Skip to content

Commit

Permalink
fix: put timezone offset into date
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Dec 5, 2024
1 parent b2bc0ad commit 34feb49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 0 additions & 4 deletions components/templates/NewsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const NewsPage: FunctionComponent<{
subtitle,
images,
videos,
release_date: date,
links,
contacts,
} = release;
Expand All @@ -47,7 +46,6 @@ const NewsPage: FunctionComponent<{
releaseDescription: description ? sanitizeHtml(description) : undefined,
links: links ? sanitizeHtml(links) : links,
contacts,
date,
images,
videos,
};
Expand Down Expand Up @@ -75,7 +73,6 @@ const NewsPage: FunctionComponent<{
subtitle,
images,
videos,
release_date: date,
more_information: moreInformation,
links,
contacts,
Expand All @@ -93,7 +90,6 @@ const NewsPage: FunctionComponent<{
: undefined,
links: links ? sanitizeHtml(links) : links,
contacts,
date,
images,
videos,
};
Expand Down
22 changes: 18 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,31 @@ function dateWoTimezone(iso) {
return new Date(iso.slice(0, -6));
}

export const useDateString = (date, isShort = false) => {
export const useDateString = (date, options = {}) => {
const { isShort = false, isCraftDate = true } = options;
const localeInfo = useGlobalData("localeInfo");
const locale = localeInfo.language || fallbackLng;
const newDate = new Date(date);
const options = {

/**
* Craft dates are stored in UTC with the timezone applied as an hours offset
* The user's timezone offset needs to be removed to restore the original date
*
* https://craftcms.com/docs/4.x/time-fields.html#converting-from-a-date-field
*/
if (isCraftDate) {
newDate.setTime(
newDate.getTime() + newDate.getTimezoneOffset() * 60 * 1000
);
}
const localeOptions = {
year: "numeric",
month: `${isShort ? "short" : "long"}`,
month: isShort ? "short" : "long",
day: "numeric",
};
let dateString = newDate.toLocaleString(locale, options);
let dateString = newDate.toLocaleString(locale, localeOptions);
isShort && (dateString = dateString.replace(",", ""));

return dateString;
};

Expand Down

0 comments on commit 34feb49

Please sign in to comment.