node.jsreactjstimezonemomentjsmoment-timezone

Convert time to UTC from desire timezone


I have to convert time to UTC time from various time zone as per requirements.

I'm using momnet with node, and I have coded as below:

const inputTime = "10:00";
const timezone = "Europe/Budapest";
const localTime = moment.tz(inputTime, timezone).utc().format("HH:mm");
console.log(localTime);

I'm getting Invalid date as o/p. Please help me to solve it.


Solution

  • You also need to supply the format of the input string you are parsing.

    const inputTime = "10:00";
    const timezone = "Europe/Budapest";
    const localTime = moment.tz(inputTime, "HH:mm", timezone).utc().format("HH:mm");
    console.log(localTime);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.38/moment-timezone-with-data-10-year-range.min.js"></script>

    Keep in mind that the input is representing the time on the current date only. The conversion may return a different value depending on when you run it.