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(NcDateTimePicker): Use ISO week numbers if showWeekNumber is set #5045

Merged
merged 1 commit into from
Jan 10, 2024
Merged
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
28 changes: 27 additions & 1 deletion src/components/NcDateTimePicker/NcDateTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default {
:append-to-body="appendToBody"
:clearable="clearable"
:format="format ? format : formatTypeMap"
:formatter="formatter"
:formatter="internalFormatter"
:lang="lang ? lang : defaultLang"
:minute-step="minuteStep"
:placeholder="placeholder ? placeholder : defaultPlaceholder"
Expand Down Expand Up @@ -353,6 +353,32 @@ export default {
formatTypeMap() {
return formatMap[this.type] ?? formatMap.date
},

/**
* The formatter used for the vue-datepicker to fix nextcloud-libraries/nextcloud-vue#5044
*/
internalFormatter() {
/**
* Get the ISO week number of the date
* @param {Date} date The date to format
*/
const getWeek = (date) => {
// Adjust to nearest Thursday
const firstThursday = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
firstThursday.setUTCDate(firstThursday.getUTCDate() + 4 - (firstThursday.getUTCDay() || 7))

const yearStart = new Date(Date.UTC(firstThursday.getUTCFullYear(), 0, 1))

// Full weeks to nearest Thursday
return Math.ceil((((firstThursday - yearStart) / 86400000) + 1) / 7)
}

return {
getWeek,
// allow to override it by users using the `formatter` prop
...(this.formatter ?? {}),
}
},
},

methods: {
Expand Down
Loading