javascriptdayjs

How to convert one time zone to another timezone using Day.js


I'm receiving data in "America/New_York" timeZone.
I want to convert that data into "Asia/Calcutta" from "America/New_York".

How to do that using Day.js ?


Solution

  • it's in the documentation here: https://day.js.org/docs/en/timezone/converting-to-zone#docsNav

    
    import dayjs from 'dayjs';
    import utc from 'dayjs/plugin/utc';
    import timezone from 'dayjs/plugin/timezone';
    
    // Extend dayjs with the plugins
    dayjs.extend(utc);
    dayjs.extend(timezone);
    
    // Example date string in "America/New_York" timezone
    const dateInNewYork = '2024-08-14T12:00:00'; // Example date and time
    
    // Convert the date to "Asia/Calcutta" timezone
    const dateInCalcutta = dayjs.tz(dateInNewYork, 'America/New_York').tz('Asia/Calcutta');
    
    // Format or display the date in "Asia/Calcutta" timezone
    console.log(dateInCalcutta.format()); // Outputs the date in "Asia/Calcutta" timezone