Sometimes, we want to convert a date to GMT with JavaScript.
In this article, we’ll look at how to convert a date to GMT with JavaScript.
Convert a Date to GMT with JavaScript
To convert a date to GMT with JavaScript, we can use the JavaScript date’s toUTCString
method to do so.
For instance, we can write:
const gmt = new Date("Fri Jan 20 2022 11:51:36 GMT-0500").toUTCString()
console.log(gmt)
to convert the JavaScript Date
instance to a GMT date string.
Therefore, gmt
is 'Thu, 20 Jan 2022 16:51:36 GMT’
.
Since the original date-time is 5 hours behind GMT, the GMT date-time is 5 hours ahead of it.
Conclusion
To convert a date to GMT with JavaScript, we can use the JavaScript date’s toUTCString
method to do so.