absent-sapphire
absent-sapphire16mo ago

Universally Set Timezone within App?

Hi, The ISO-8601 dates in my database are in UTC. However, my application is using a different timezone, causing incorrect dates to be shown. I'd rather not manually swizzle each date-related field (i.e DateField, DatePicker); is there a way I can universally set the timezone to UTC?
7 Replies
kapa.ai
kapa.ai16mo ago
Hi @avangeous. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. However, you might find it helpful to look into the Temporal API, which includes a Temporal.TimeZone data type that can be used to specify a particular time zone. Here's an example from the Temporal API documentation (https://tc39.es/proposal-temporal/docs/):
const timeZone = Temporal.TimeZone.from('America/Chicago')

console.log(timeZone.toString())
// America/Chicago

const localTimeZone = Temporal.Now.timeZone()

console.log(localTimeZone.toString())
// Asia/Karachi
const timeZone = Temporal.TimeZone.from('America/Chicago')

console.log(timeZone.toString())
// America/Chicago

const localTimeZone = Temporal.Now.timeZone()

console.log(localTimeZone.toString())
// Asia/Karachi
This might be a good starting point for managing time zones in your application. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai16mo ago
<@1122927158348763206> kapa.ai is still learning and improving, please let us how it did by reacting below
absent-sapphire
absent-sapphire16mo ago
@kapa.ai How can I set the timezone of my app?
kapa.ai
kapa.ai16mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Based on the information provided in the extracts, you can use the Temporal API to manage time zones in your JavaScript application. The Temporal API includes a Temporal.TimeZone data type that can be used to specify a particular time zone. Here's an example:
const timeZone = Temporal.TimeZone.from('America/Chicago')

console.log(timeZone.toString())
// America/Chicago

const localTimeZone = Temporal.Now.timeZone()

console.log(localTimeZone.toString())
// Asia/Karachi
const timeZone = Temporal.TimeZone.from('America/Chicago')

console.log(timeZone.toString())
// America/Chicago

const localTimeZone = Temporal.Now.timeZone()

console.log(localTimeZone.toString())
// Asia/Karachi
You can use Temporal.TimeZone.from() to set a specific time zone, and Temporal.Now.timeZone() to get the current time zone. However, please note that the Temporal API is currently at stage 3 and not yet supported by browsers. You can use a polyfill like @js-temporal/polyfill to try it out. For more details, you can refer to the Temporal API documentation (https://tc39.es/proposal-temporal/docs/). Please note that this is a general JavaScript solution. If you're asking about setting the time zone in a specific framework or library (like Refine), the approach might be different and isn't covered in the provided extracts. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai16mo ago
<@1122927158348763206> kapa.ai is still learning and improving, please let us how it did by reacting below
absent-sapphire
absent-sapphire16mo ago
I ended up just; 1) swizzling DateField and adding .utc() to the dayjs, and 2) adding format={date => date.utc().format(/*FORMAT HERE*/)} to datepickers. How the datepicker will work in regards to writes i still have to see/debug
constant-blue
constant-blue7mo ago
Thanks for this! It helped me a lot. Testing with my database, it is correctly saving the dates without a timezone offset.