Difference between toISOString and toUTCstring in Javascript Date Class.
JavaScript Date
class has lots of methods for manipulating date. I find myself usually going back to which to use between ISO date format and UTC date format. I hope this helps that developer who experience the same thing to know what these date methods are and when to use which.
What is toISOString()
?
The toISOString()
is a method of JavaScript Date instance. It returns a string that represents date and time in a simplified version of ISO 8601 format. ISO 8601 is an International Standard for representing Date and Time, and toISOString()
is the JavaScript method meant to represent this Standard.
If you want to know more about the ISO 8601 format, I’ll link to resources at the end of this article. I’ll recommend reading the medium post by @biseldev. I’ll add a link to his article at the end of this article.
What is toUTCstring()?
The toUTCString()
method is a JavaScript Date method that returns date and time in the RFC 7231 format. To put simply toUTCString()
is a JavaScript Date method that returns date using the Universal TimeZone. toUTCString()
is similar to toISOString()
in the sense that both methods return date and time, but in different formats.
REPRESENTATION OF toISOString()
AND toUTCString()
When you use either of these methods, they return date and time in different formats. Let’s analyze this:
toISOString()
REPRESENTATION
toISOString()
returns date in this format: ±YYYY-MM-DDTHH:mm:ss.sssZ
where each part of this string stand for:
I had to use an image since medium doesn’t allow markdown tables to show the representation.
Examples:
- 2024–05–18T12:34:46.010Z — An example without an offset.
- 2024–05–18T12:40:40+07:00 — An example with an ahead offset
- 2024–05–18T12:44:20−07:00 — An example with a behind offset.
- 2021–10–05T14:48:00.000Z — An example without an offset
toUTCString()
REPRESENTATION
The toUTCString()
returns date in a more text-like manner than the toISOString()
. Here’s an example. Sat, 18 May 2024 12:41:43 GMT
. Looking at this, you could tell what’s being returned by JavaScript Date. But let’s break it down still. toUTCString()
do exist in the below format.
There’s not much else to say about the toUTCString()format. I believe it is understandable using this table.
BROWSER COMPATIBILITY
There’s one more thing I want to add about the differences between these two methods. The Browser Compatibility.
In terms of browser compatibility. Both methods have full compatibility on all major browsers — Desktop, Mobile and Server. But toUTCString()
had earlier compatibility before the toISOString()
method. I just thought to mention this for someone who might need it.
CONCLUSION
JavaScript Date
has lots of methods for manipulating date. I wrote this article because I find myself usually going back to which method to use between ISO date format and UTC format. But I believe this article breakdown has helped someone to retain when to use which method and for which scenario.
I believe that you have learnt something from this article.
Thank you reading. 🙂
For further reading, check out the below links:
(https://medium.com/@biseldev/understanding-iso-8601-and-utc-ce8d99609055)
https://www.iso.org/iso-8601-date-and-time-format.html
https://en.wikipedia.org/wiki/ISO_8601
https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.1