Hello everone 🖐
I have an app that collects the birthdays of people and represent it in a repeater, the problem is that exctracted value from the date always differ that what it actually is, here's what I mean.
![](https://static.wixstatic.com/media/a27d24_f567ed221de949229239a256b43eba06~mv2.png/v1/fill/w_429,h_333,al_c,q_85,enc_auto/a27d24_f567ed221de949229239a256b43eba06~mv2.png)
Here's the code that produces this:
let birthdayToday = [];
const today = new Date();
const day = today.toLocaleDateString('en-US', {day: 'numeric'})
const month = today.toLocaleDateString('en-US', {month: 'short'})
console.warn(`Today is ${day} ${month} 2020`)
contacts.items.forEach(contact => {
let contactDay = contact.birthday.toLocaleDateString('en-US', {day: 'numeric'})
let contactMon = contact.birthday.toLocaleDateString('en-US', {month: 'short'})
console.log(`Contact birthday: ${contactDay} ${contactMon}`)
if ( (day === contactDay) && (month === contactMon) ) { birthdayToday.push(contact) }
})
console.info(birthdayToday)
The first three (3) items in the database have their day to 9, which is today, but when getting their value they become 8 is in the console, and obviously, not items were pushed in the new array.
I did also the getDate() and getMonth() methods but they gave the same weird behaviour - a one month before (notice the picture below).
![](https://static.wixstatic.com/media/a27d24_9d192c212464452cb99a6a006c66d194~mv2.png/v1/fill/w_185,h_326,al_c,q_85,enc_auto/a27d24_9d192c212464452cb99a6a006c66d194~mv2.png)
Also notice that the day that was assigned by code (not extracted from the collection) was correct in both methods. The issue occure when pulling data from the database.
Any thoughts? Is it a bug or what?
Thanks in advance.
Ahmad
Looked for it, and all I found was this updated setting, which I suppose covers the time zone that all collections on the site use:
Dashboard > Settings > Language & Region > Site Region > Time Zone
You are definitely stumbling over time zone issues.
First, in the site settings, you can pick UTC times or local times for the collection DATE/TIME entries. I highly recommend to use UTC.
Then, get the user's browser time offset from UTC.
https://www.w3schools.com/JSREF/jsref_gettimezoneoffset.asp
The getTimezoneOffset() method returns the time difference between UTC time and local time, in minutes.
Adjust the collection DATE/TIME object with the getTimezoneOffset() minutes.
So, for each DOB you want to use, you'd have to do something like this:
(you can abbreviate this code as needed - complete breakdown for demo)
// ====================================================== // get user dob // ====================================================== // user.dob is the value from the collection let userDob1 = new Date(user.dob); //console.log("stored user dob from db: " + userDob1); // set dob utc let dob = new Date(userDob1); let offsetMinutesDOB = dob.getTimezoneOffset(); let offsetHoursDOB = offsetMinutesDOB/60; let utcYearDOB = dob.getUTCFullYear(); let utcMonthDOB = dob.getUTCMonth(); // 0 index let utcDateDOB = dob.getUTCDate(); let utcHoursDOB = dob.getUTCHours(); let utcMinutesDOB = dob.getUTCMinutes(); var utcDOB = new Date(Date.UTC(utcYearDOB,utcMonthDOB,utcDateDOB,offsetHoursDOB)); user.dob = utcDOB; // ================== end of get DOB ================
This way, no matter what time zone your user is in, you are always displaying the correct dates:
UTC from collection + UTC time zone offset (for DOB, etc.).
When writing dates to the collection, you can ensure proper time offset with a before insert hook.
For testing with a Windows machine, change the OS time and observe how the browser time changes. With the proper offset applied, the intended date does not change.
Good luck.
console log the birthday column value directly with console.log(birthday)
If it shows a string like 'Indian Standard Time' then as JD said, its about timezone.
It's probably a matter of time zone.