How to convert UTC to another timezone in MongoDB with NestJs

314 Views Asked by At

We have a requirement of converting a date received in UTC time zone to EST or CST etc; So we receive a serires of events with a dateTime in UTC. We would need to loop the events and perform some calculations at midnight depending upon the time zone at midnight. We get all the list of events for a specific timezone and perform some calculations. I am trying to figure out the best possible way to do this. We have mongodb and NestJS with mongoose library. I tried converting the UTC to EST or whatever timezone using date-fns-tz library and even tried with moment the output is a date in string while we have the eventDateTime as Date type and this saves the date as UTC not EST. 1)It is better to save the eventDateTime by converting it to a specific timezone and perform the calculations? 2)Or anything else. Please advise.

So here is the sample json I have and collection let's say eventsCollection

``

{
 "code": "DRL-0014",    
 "status": "Training",
 "sourceSystem": "sourceSystem",
 "traineeCode":"EMP4510",
 "eventCode": "EVT-1012",
 "eventDateTime": "2023-08-16T02:00:00.000Z",
 "remark": "remark",
 "clock": { 
   "timeRemaining": 21600
 },   
},
{
 "code": "DRL-0014",    
 "status": "Training",
 "sourceSystem": "sourceSystem",
 "traineeCode":"EMP4510",
 "eventCode": "EVT-1012",
 "eventDateTime": "2023-08-16T03:00:00.000Z",
 "remark": "remark",
 "clock": { 
   "timeRemaining": 18000
 },   
},

``

So, we will have a series of events and we need to rely on the timezone to calculate a series of events that occurred in 24 hours cycle. So, we calculate them from midnight until midnight(24 hours) and save as single document and since there is a gap of 4 hours b/w utc and est and 5 hours between utc which means few events may fall in another day if we rely on utc for the calculations so we need to convert the eventDateTime to EST or CST or whatever timezone saved in our different collection and have inserted in eventsCollection collection with a new field called startDateTime. So, "eventDateTime": "2023-08-16T03:00:00.000Z", conversion of EST will be "eventDateTime": "2023-08-15T23:00:00-04:00", or simply 2023-08-15T23:00:00. Also, we will be having thousands of documents to calculate every midnight for almost many timezones. The converted documents looks like below with an additional field ``

{
 "code": "DRL-0014",    
 "status": "Training",
 "sourceSystem": "sourceSystem",
 "traineeCode":"EMP4510",
 "eventCode": "EVT-1012",
 "eventDateTime": "2023-08-16T02:00:00.000Z",
 "shiftStartDateTime": "2023-08-15T22:00:00.000",
 "remark": "remark",
 "clock": { 
   "timeRemaining": 21600
 },   
},
{
 "code": "DRL-0014",    
 "status": "Training",
 "sourceSystem": "sourceSystem",
 "traineeCode":"EMP4510",
 "eventCode": "EVT-1012",
 "eventDateTime": "2023-08-16T03:00:00.000Z",
 "shiftStartDateTime": "2023-08-15T23:00:00.000",
 "remark": "remark",
 "clock": { 
   "timeRemaining": 18000
 },   
},

`` Also, the job which runs once will calculate based on the shiftStartDateTime will save the result in another collection for each "traineeCode"

``

{
 "code": "DRL-0014",    
 "status": "Training",
 "traineeCode":"EMP4510",
 "eventCode": "EVT-1012",
 "shiftStartDateTime": "2023-08-15T22:00:00.000",
 "remark": "remark",
 "totalTime:"18000" // Since 21600-3600=18000(which is 5 hours)
},
{
 "code": "DRL-0015",    
 "status": "Training",
 "traineeCode":"EMP4511",
 "eventCode": "EVT-4012",
 //....
},
...and so on

`` In javascript I tried doing something like const estTimestamp = moment(event.eventDateTime).tz('America/New_York').toDate();

0

There are 0 best solutions below