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

fix: put timezone offset into date #622

Merged
merged 1 commit into from
Dec 5, 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
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