How to Obtain the `startOfDay` in a `Node.js` Application Running on a UTC Timezone Server

47 Views Asked by At

I'm trying to get the startOfDay of IST (Indian Standard Time) in UTC, on a node app running on ubuntu server where timezone is set to UTC

  async onModuleInit() {
    const currentUtcTime = new Date();

    // Without date-fns-tz
    const startOfDay1 = startOfDay(currentUtcTime);

    // With date-fns-tz
    const convertedTime = utcToZonedTime(currentUtcTime, 'Asia/Kolkata');
    const startOfDay2 = startOfDay(convertedTime);

    console.log('currentTime :', currentUtcTime);
    console.log(`startDate1 :`, startOfDay1);
    console.log(`startDate2 :`, startOfDay2);
  }

this is the result i get

currentTime : 2023-09-25T14:46:08.192Z
startDate1 : 2023-09-25T00:00:00.000Z
startDate2 : 2023-09-25T00:00:00.000Z

but the start date should be 2023-09-24T18:30:00.000Z. if i set my TZ='Asia/Kolkata' and run the node app, i do get 2023-09-24T18:30:00.000Z but my server runs on UTC.

Am i handling it the right way? can someone guide me here?

0

There are 0 best solutions below