diff --git a/docs/src/pages/quasar-utils/date-utils.md b/docs/src/pages/quasar-utils/date-utils.md index b36e4bc0795..3f8c2e52a7a 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/date.js b/ui/src/utils/date/date.js index eed8b34d8b5..6d8d5726edf 100644 --- a/ui/src/utils/date/date.js +++ b/ui/src/utils/date/date.js @@ -509,6 +509,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() } @@ -762,6 +772,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 @@ -1017,6 +1043,7 @@ export default { buildDate, getDayOfWeek, getWeekOfYear, + getISOWeekYear, isBetweenDates, addToDate, subtractFromDate,