Skip to content

Commit 65fcdc5

Browse files
committed
fix: get localtime in a ISO-like string
native browser Date doesn't let you get local time, so need this workaround
1 parent 804a86e commit 65fcdc5

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/core/stores/smilestore.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ if (seed) {
7070
)
7171
}
7272

73+
/**
74+
* Returns the current local time as an ISO-like string in the user's timezone
75+
* @returns {string} Local time string (e.g., "2024-12-03T16:04:00.000-05:00")
76+
*/
77+
function getLocalTimeString() {
78+
const now = new Date()
79+
const offset = -now.getTimezoneOffset()
80+
const sign = offset >= 0 ? '+' : '-'
81+
const pad = (n) => String(Math.abs(n)).padStart(2, '0')
82+
const offsetHours = pad(Math.floor(Math.abs(offset) / 60))
83+
const offsetMins = pad(Math.abs(offset) % 60)
84+
85+
const year = now.getFullYear()
86+
const month = pad(now.getMonth() + 1)
87+
const day = pad(now.getDate())
88+
const hours = pad(now.getHours())
89+
const minutes = pad(now.getMinutes())
90+
const seconds = pad(now.getSeconds())
91+
const ms = String(now.getMilliseconds()).padStart(3, '0')
92+
93+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${ms}${sign}${offsetHours}:${offsetMins}`
94+
}
95+
7396
/////// continue with setting up smilestore ///////
7497
/**
7598
* Determines the initial route based on application mode
@@ -318,7 +341,7 @@ export default defineStore('smilestore', {
318341
this.browserPersisted.consented = true
319342
this.data.consented = true
320343
this.data.starttime = fsnow()
321-
this.data.starttimeLocal = new Date().toISOString()
344+
this.data.starttimeLocal = getLocalTimeString()
322345
this.data.userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone
323346
this.data.userTimezoneOffset = new Date().getTimezoneOffset()
324347
},
@@ -343,7 +366,7 @@ export default defineStore('smilestore', {
343366
this.data.withdrawn = true
344367
this.private.withdrawData = forminfo
345368
this.data.endtime = fsnow()
346-
this.data.endtimeLocal = new Date().toISOString()
369+
this.data.endtimeLocal = getLocalTimeString()
347370
},
348371

349372
verifyVisibility(value) {
@@ -360,7 +383,7 @@ export default defineStore('smilestore', {
360383
this.browserPersisted.done = true
361384
this.data.done = true
362385
this.data.endtime = fsnow()
363-
this.data.endtimeLocal = new Date().toISOString()
386+
this.data.endtimeLocal = getLocalTimeString()
364387
},
365388

366389
/**

0 commit comments

Comments
 (0)