How to convert a date to ISO 8601 format with time zone offset in JavaScript?

Sometimes, we want to convert a date to ISO 8601 format with time zone offset in JavaScript.

In this article, we’ll look at how to convert a date to ISO 8601 format with time zone offset in JavaScript.

How to convert a date to ISO 8601 format with time zone offset in JavaScript?

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.

For instance, we write

const d = new Date().toLocaleString("sv", { timeZoneName: "short" });

to call toLocaleString with the locale string and { timeZoneName: "short" } to return a date time string formatted into ISO format for the given locale.

We set timeZoneName to 'short' to add the short time zone string to the date time string.

Conclusion

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.