1111 <NcDateTimePickerNative :id =" inputId "
1212 type="date"
1313 label=""
14- :value =" value "
14+ :value =" timezoneAdjustedValue "
1515 @input =" onInput " />
1616
1717 <p class =" property__helper-text-message" >
@@ -33,6 +33,16 @@ import HeaderBar from './shared/HeaderBar.vue'
3333
3434const { birthdate } = loadState (' settings' , ' personalInfoParameters' , {})
3535
36+ /**
37+ * Convert a birthdate string value into a Date.
38+ *
39+ * @param {string } birthdateValue - e.g. "1987-12-01" or "1987-12-01T00:00:00.000Z"
40+ * @return {Date}
41+ */
42+ function birthdateValueToDate (birthdateValue ) {
43+ return new Date (birthdateValue)
44+ }
45+
3646export default {
3747 name: ' BirthdaySection' ,
3848
@@ -44,7 +54,7 @@ export default {
4454 data () {
4555 let initialValue = null
4656 if (birthdate .value ) {
47- initialValue = new Date (birthdate .value )
57+ initialValue = birthdateValueToDate (birthdate .value )
4858 }
4959
5060 return {
@@ -60,23 +70,35 @@ export default {
6070 inputId () {
6171 return ` account-property-${ birthdate .name } `
6272 },
63- value: {
64- get () {
65- return new Date (this .birthdate .value )
66- },
67- /** @param {Date} value The date to set */
68- set (value ) {
69- const day = value .getDate ().toString ().padStart (2 , ' 0' )
70- const month = (value .getMonth () + 1 ).toString ().padStart (2 , ' 0' )
71- const year = value .getFullYear ()
72- this .birthdate .value = ` ${ year} -${ month} -${ day} `
73- },
73+ value () {
74+ return birthdateValueToDate (this .birthdate .value )
75+ },
76+ /**
77+ * Adjusted value for usage with `NcDateTimePickerNative` (internally `<input="date">`)
78+ * The saved value is is UTC and we want to show it the same regardless of the browsers/OSs timezone.
79+ * When the adjusted value is displayed and the users timezone is applied, this adjusted value then looks like the UTC value.
80+ */
81+ timezoneAdjustedValue () {
82+ // example: this.birthdate.value === '1987-12-01T00:00:00.000Z' or '1987-12-01'
83+
84+ // example: Mon Nov 30 1987 16:00:00 GMT-0800 (Pacific Standard Time)
85+ // `NcDateTimePickerNative` would show this as 11/30/1987
86+ const date = this .value
87+ const timezoneOffsetMilliseconds = date .getTimezoneOffset () * 60 * 1000
88+ const adjustedDate = new Date (date .getTime () + timezoneOffsetMilliseconds)
89+
90+ // example: Tue Dec 01 1987 00:00:00 GMT-0800 (Pacific Standard Time)
91+ // `NcDateTimePickerNative` will show this as 12/01/1987
92+ return adjustedDate
7493 },
7594 },
7695
7796 methods: {
7897 onInput (e ) {
79- this .value = e
98+ const day = e .getDate ().toString ().padStart (2 , ' 0' )
99+ const month = (e .getMonth () + 1 ).toString ().padStart (2 , ' 0' )
100+ const year = e .getFullYear ()
101+ this .birthdate .value = ` ${ year} -${ month} -${ day} `
80102 this .debouncePropertyChange (this .value )
81103 },
82104
0 commit comments