Convert Local Timezone time to UTC
utc time converter
time converter calculator
time conversion chart
I'd like to convert this to a date object in
const timezone = 'America/New_York'; const localMidnight = new Date(Date.UTC(2019, 0, 4)) // 2019-01-04T00:00:00.000Z moment.tz(localMidnight, timezone).utc().toDate()
Here this is still returning the same as the input
> m(localMidnight, 'America/New_York').tz('utc').toDate() 2019-01-04T00:00:00.000Z > m(localMidnight, 'America/New_York').tz('UTC').toDate() 2019-01-04T00:00:00.000Z > m(localMidnight, 'America/New_York').utc().toDate() 2019-01-04T00:00:00.000Z
It seems like the problem was with the format of the date you created, in this example you can see that
.toISOString() adds a Z at the end, and for the
moment.tz method to work you need to remove that letter from the format.
Here's an example creating a new date in
'America/New_York' and changing the time zone to UTC
const timezone = 'America/New_York'; const localMidnight = new Date(2019, 01, 04); console.log("Original time:") console.log(localMidnight.toISOString()); console.log("Converted to UTC:"); console.log(moment.tz(localMidnight.toISOString().slice(0, -1), timezone).utc().format());
Time Zone Converter, Please note this world time conversion can only make predictions based on current time zone rules, so if you are checking a date far into the future, be sure to UTC Time Zone Converter. This UTC time zone converter is an effective and convenient tool for everyone who needs to know the current time in a certain time zone. This converter can show the time difference between UTC time zone and another selected time zone. It can show the most accurate time now at any selected point on earth.
What I think you want is to create a date based on a date and time in a particular timezone, then create an ISO 8601 timestamp for UTC.
Comments in the following should be sufficient, but ask if you need more help.
// Create a moment for midnight in America/New_York var newYork = moment.tz("2019-01-04 00:00", "America/New_York"); // Create an ISO 8601 timestamp UTC console.log(newYork.utc().format()); // 2019-01-04T05:00:00Z
How to translate UTC to your time | Astronomy Essentials, Look here to learn how to translate UTC to your local time zone. A chart to convert contiguous U.S. time to 24-hour-format UTC, and vice It's just a date and time that can be in any time zone. When you call TimeZoneInfo.ConvertTimeToUtc(dt, TimeZoneInfo.Local), you are telling it which time zone it starts in. So if you start in Central, you will get one answer, whereas if you start in Eastern, you will get an answer that is an hour earlier, UTC. Indeed, this is what your code shows.
To achieve expected result, use below option
Syntax - moment(date).tz(timezone).utc().format()
For format-YYYY-MM-DDTHH:mm:ss.SSSZ use toISOString() instead of format()
const timezone = 'America/New_York'; const localMidnight = new Date(Date.UTC(2019, 0, 4)) console.log("newTime in utc", moment(localMidnight).tz(timezone).utc().format()) console.log("newTime", moment(localMidnight).tz(timezone).utc().toISOString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>
Time Zone Converter, Time Zone List: All Time Zones Sort List by Country. Use Current Date/Time Select this option to override date/time values below. Use The Following Date/Time (UTC/GMT) Time Zone Converter If you have a web cast, online chat, conference call or other live event where people from all over the world want to attend, this (UTC/GMT) time zone difference converter lets you offer everyone an easy way to determine their own local time and date for your live event in (UTC/GMT) .
Time Zone Converter – Time Difference Calculator, Cause remember your application can have multiple servers located in different timezones. You would not want to store date in local timezone Universal Time ( UTC ) to Your Local Time and Worldwide Time Conversions, Conversion Time Chart between Universal Time and Local Time
TimeZoneInfo.ConvertTimeToUtc Method (System), Learn to convert times between from one time zone to another in . The following code converts the current local time to UTC and displays the To convert UTC to the time in any time zone that you designate, call the ConvertTimeFromUtc method. The method takes two parameters: The UTC to convert. This must be a DateTime value whose Kind property is set to Unspecified or Utc.
- Is this what you're trying to do? stackoverflow.com/questions/10087819/…
- Updated, I tried this is not working as well.
new Date(2019,0,1).toISOString()to get the format you posted
- This question is confusing :-) A date string with
Zat the end is already UTC, not local. Do you have an actual date object or a string? Why would you use
Date.UTCto get a non-UTC date? An actual date object is already aware of its UTC time, use
.toISOStringto see it.
- I don't know what you think this is doing, but it seems wrong. I get "Original time: 2019-02-03T14:00:00.000Z" which is correct, then "Converted to UTC: 2019-02-03T19:00:00Z" which is wrong because the first string is already UTC. The second is just shifted 5 hours. The equivalent time in New York would be 2019-02-03T09:00:00.000-05:00. Or midnight at the start of 2019-01-04 in New York is 2019-01-04T05:00:00Z.
- I get "Error: Moment Timezone has no data for America/New_York.". Your localMidnight is not local midnight unless the host is set to UTC,
new Date(Date.UTC(2019, 0, 4))creates a Date for 2019-01-04T00:00:00Z.
- @RobG, fixed that error which was due to missing moment-timezone-with-data js file
- You can remove moment-timezone.min.js. The code still does nothing useful,
new Date(Date.UTC(2019, 0, 4))creates a date for midnight UTC, not local. Using moment.js's .utc method means all calculations and methods use UTC and the default output format is ISO 8601. You'll get exactly the same result without moment.js and using toISOString instead.