Search code examples
javascriptdatetimemomentjsmoment-timezone

Get Time in GMT based on the parameter Time & Timezone


I need to schedule a job daily at 1:30 AM, Here 1:30 AM is Timezone dependent.

For Example:
America/Chicago - 1:30 AM output should be GMT 06:30.
Asia/Kolkata - 1:30 AM output should be GMT 20:00.
Asia/Kabul - 1:30 AM output should be GMT 21:00

I want to create a function that will take Timezone (America/Chicago) and Base Time (1:30) as input and output should be 06:30 in GMT


Solution

  • // const moment = reqimpuire("moment-timezone");
    
    function toTimeZone(zone) {
      const time = moment.tz("2013-11-18 01:30:00", zone);
      const utc = moment.utc(time).format();
      console.log(utc);
    }
    toTimeZone("Africa/Tripoli");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.9/moment-timezone-with-data.min.js"></script>

    I solved the problem using the above code.