From c6fcfc8380c6fe385619a9c1990e9f52ffade709 Mon Sep 17 00:00:00 2001 From: Porter Severtson Date: Thu, 4 Apr 2024 11:41:58 -0700 Subject: [PATCH] Format Date - Support ISO Week Year (GGGG) Co-Authored-By: Dylan Lathrum <19830705+dylancyclone@users.noreply.github.com> Co-Authored-By: Joseph Hale, MS SE <47901316+thehale@users.noreply.github.com> --- docs/src/pages/quasar-utils/date-utils.md | 11 +++++++++ ui/src/utils/date.js | 27 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/docs/src/pages/quasar-utils/date-utils.md b/docs/src/pages/quasar-utils/date-utils.md index 1be66a9f5291..1fad8e03f237 100644 --- a/docs/src/pages/quasar-utils/date-utils.md +++ b/docs/src/pages/quasar-utils/date-utils.md @@ -67,6 +67,7 @@ Available format tokens: | Day of Week | | | Day of Week (ISO) | | | Week of Year | | +| ISO Week Year | | | Hour | | | Minute | | | Second | | @@ -307,6 +308,16 @@ const newDate = new Date(2017, 0, 4) const week = date.getWeekOfYear(newDate) // `week` is 1 ``` +To get the [ISO week year](https://en.wikipedia.org/wiki/ISO_week_date) for a given date object use: + +```js +import { date } from 'quasar' + +const newDate = new Date(2022, 0, 1) // End of the last week of 2021 +const year = date.getISOWeekYear(newDate) // `year` is 2021 +const week = date.getWeekOfYear(newDate) // `week` is 52 +``` + To get the day number in year for a given date object use: ```js diff --git a/ui/src/utils/date.js b/ui/src/utils/date.js index 70d5da6d3bd7..c928eeb4df01 100644 --- a/ui/src/utils/date.js +++ b/ui/src/utils/date.js @@ -497,6 +497,16 @@ export function getWeekOfYear (date) { return 1 + Math.floor(weekDiff) } +export function getISOWeekYear (date) { + // Remove time components of date + const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate()) + + // Change date to Thursday same week + thursday.setDate(thursday.getDate() - ((thursday.getDay() + 6) % 7) + 3) + + return thursday.getFullYear() +} + function getDayIdentifier (date) { return date.getFullYear() * 10000 + date.getMonth() * 100 + date.getDate() } @@ -750,6 +760,22 @@ const formatter = { : date.getFullYear() }, + GG (date, dateLocale, forcedYear) { + // workaround for < 1900 with new Date() + const g = this.GGGG(date, dateLocale, forcedYear) % 100 + return g >= 0 + ? pad(g) + : '-' + pad(Math.abs(g)) + }, + + // Year: 1900, 1901, ..., 2099 + GGGG (date, _dateLocale, forcedYear) { + // workaround for < 1900 with new Date() + return forcedYear !== void 0 && forcedYear !== null + ? forcedYear + : getISOWeekYear(date) + }, + // Month: 1, 2, ..., 12 M (date) { return date.getMonth() + 1 @@ -985,6 +1011,7 @@ export default { buildDate, getDayOfWeek, getWeekOfYear, + getISOWeekYear, isBetweenDates, addToDate, subtractFromDate,