Skip to content

Commit

Permalink
Format Date - Support ISO Week Year (GGGG)
Browse files Browse the repository at this point in the history
Co-Authored-By: Dylan Lathrum <19830705+dylancyclone@users.noreply.github.com>
Co-Authored-By: Joseph Hale, MS SE <47901316+thehale@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 4, 2024
1 parent d3b1799 commit c6fcfc8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/src/pages/quasar-utils/date-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Available format tokens:
| Day of Week | <ul><li>**d**: 0 1 ... 5 6</li><li>**dd**: Su Mo ... Fr Sa</li><li>**ddd**: Sun Mon ... Fri Sat</li><li>**dddd**: Sunday Monday ... Friday Saturday</li></ul> |
| Day of Week (ISO) | <ul><li>**E**: 1 2 ... 6 7</li></ul> |
| Week of Year | <ul><li>**w**: 1 2 ... 52 53</li><li>**ww**: 01 02 ... 52 53</li></ul> |
| ISO Week Year | <ul><li>**GG**: 70 71 ... 29 30</li><li>**GGGG**: 1970 1971 ... 2029 2030</li></ul> |
| Hour | <ul><li>**H**: 0 1 ... 22 23</li><li>**HH**: 00 01 ... 22 23</li><li>**h**: 0 ... 11 12</li><li>**hh**: 01 02 ... 11 12</li></ul> |
| Minute | <ul><li>**m**: 0 1 ... 58 59</li><li>**mm**: 00 01 ... 58 59</li></ul> |
| Second | <ul><li>**s**: 0 1 ... 58 59</li><li>**ss**: 00 01 ... 58 59</li></ul> |
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions ui/src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -985,6 +1011,7 @@ export default {
buildDate,
getDayOfWeek,
getWeekOfYear,
getISOWeekYear,
isBetweenDates,
addToDate,
subtractFromDate,
Expand Down

0 comments on commit c6fcfc8

Please sign in to comment.