Skip to content

How to Convert a JavaScript Date to UTC

Assume the user is in a timezone quite different from UTC, and the date range should be converted to UTC.

This can be done by using the JavaScript Date object. To convert a JavaScript date to UTC, you should use the toISOString() method:

JavaScript: Convert a Date to UTC

Output appears here after Run.

INFO

The toISOString() method returns a string that represents the given date in the ISO 8601 format according to universal time.

The toISOString() method returns a string in extended ISO format (ISO 8601). The timezone is zero UTC offset, as indicated by the suffix "Z". It does not take any parameters and is only used with a Date object. Note that toISOString() formats the timestamp as UTC but does not modify the underlying Date object's internal millisecond value.

Date Object

The JavaScript Date object represents a single instance in time in a platform-independent format. It contains a number that represents milliseconds since January 1, 1970, UTC. There are a number of methods that obtain a date in various formats, as well as to perform time zone conversions. There are also functions that output the date and time in UTC (Coordinated Universal Time), the global standard time defined by the World Time Standard.

Dual-run preview — compare with live Symfony routes.