Search code examples
javascriptdatetimedaterangepicker

Javascript local datetime into selected timezone


I have https://www.daterangepicker.com/ which outputs some datetimes.

If I am in timezone (device timezone) Europe/Kiev (GMT+3) the outputs will be in those timeszones. For example 27 June 2022 12:00:00 am GMT+3. (midnight) enter image description here

var output = 1656277200000;

I also have the user that selects a timezone for example user selects London (GMT+1).

var timezone = "Europe/London";

I want to convert that output unix time from 27 June 2022 12:00:00 am GMT+3 to the timezone of what the user selected, the London timezone: 27 June 2022 12:00:00 am GMT+1. That means the unix: 1656284400000

enter image description here


Solution

  • You can try this way

    // Without moment-timezone
    new Date().toLocaleString("en-US", { timeZone: "America/New_York" });
    
    // With moment-timezone
    var moment = require("moment-timezone");
    const today = new Date();
    var timeGet = moment(today);
    timeGet.tz("Asia/Karachi").format("ha z");