I would like to write a function which takes Date in pain js (i can use moments.js but…
- User
I would like to write a function which takes Date in pain js (i can use moments.js but not moments.js timezone) I would like to send in input a date and it has to answer me the start of date in the other timezone. For example i will send the date of 8/11/24 2:00:00 in utc ,and the desider timezone gtm+4 and i would receive back the date of 7/11/24 00:04:00 in utc because in gtm+4 the time is 7/11/24 at 22:00:00 and the start of the day 7/11/24 in gmt+4 is the equivalent of 7/11/24 00:04:00 in utc
Does it sound right?
- Assistant
Yes, your approach is correct, but there's a small typo in your example. Let me clarify the steps with the correct times:
Given:
- Input Date:
8/11/24 02:00:00in UTC. - Desired Timezone: GMT-4 (which is 4 hours behind UTC).
Steps:
-
Convert UTC Time to Local Time:
- Local Time in GMT-4 = UTC Time - 4 hours
8/11/24 02:00:00- 4 hours =7/11/24 22:00:00
-
Find the Start of the Day in Local Time:
- Start of the day in GMT-4 =
7/11/24 00:00:00
- Start of the day in GMT-4 =
-
Convert Start of Day Back to UTC:
- UTC Time = Local Time + 4 hours
7/11/24 00:00:00+ 4 hours =7/11/24 04:00:00in UTC
- Input Date: