Create a ISO String from date text input intended for UTC date in JavaScript

Create a ISO String from date text input intended for UTC date in JavaScript

Let’s say that you receive a date like “14/01/2022”, “14 January 2022”, “Jan, 14 2022” etc, and you need to convert this date to an ISO string in JavaScript while ensuring that the date that is inputted is for UTC (Universal Time Coordinated) timezone – no matter where you are in the world. It would seem easy right?

Your first reaction might be to simply do something like this:

Now unless you are sitting smack-dab in a UTC timezone you might be in for a bit of a surprise.

Right now, my timezone is UTC+11 hours. This means that my result of the code in the example above will report the previous day at 1pm UTC.

That’s not what I am looking for all. I need to set this date to precisely midnight of 14 Jan 2022 UTC time.

The ISO String we need is this: 2022-01-14T00:00:00.000Z

The Solution

Note! Parsing an ISO format (e.g. “2022-01-14”) using this approach will not work because it will already be set to UTC. Simply use new Date(dateString).toISOString(); in this case. 

Tested in Chrome and Node.js.

 

Get your timezone offset

Line 7

First, we need to get the current timezone offset that you are working in. We can do this with the JavaScript Date getTimeZoneOffset() method.

Why not just manually add 11 hours to the date? Well, two reasons come to mind:

  1. There is daylight saving where I am currently working in so this offset will change to +10 in wintertime.
  2. What if this code is to be used from different locations? We want all locations to be able to set a date to UTC time.

The getTimeZoneOffset() method returns the amount of time offset from UTC in minutes. So for example, our timezone is +11 hrs from UTC so:

UCT 0hrs - UTC+11hrs = -11hrs.

-11hrs x 60 minutes  = -660 minutes. 

Converting the Date String to UTC time

Line 8

Now that we have our offset, we can use the JavaScript Date.parse method to create our local date in milliseconds and add the offset, converted to milliseconds to set the date in UTC time.

Date.parse(dateString) - (offset * 60 * 1000);

Note! You could alternatively use the new Date() constructor instead of the date parse method here. However, the date parse approach did run a little faster on a JavaScript benchmark test.

Convert UTC date in milliseconds to ISO date.

Line 9

Finally, we can use the toISOString() method to convert the UTC date in milliseconds to a readable string.


Finally, the ISO string is returned from the function.

The video tutorial

Conclusion

The above is a rather unusual situation. More often than not you will be receiving or generating a proper date format rather than date text strings. Likewise, you generally won’t need to get a UTC time based on a current date either.

In my research for this post, I did find that there is an approach to use Date.UTC(year, month, day), but that would require a lot more code to identify years months and days in non-uniform dates and then split them and add them to the UTC arguments. So I abandoned it.

It would be cool to hear how you use this to solve a problem in your own project? Was it unavoidable? Did you discover a different way?

Feel free to comment below.

Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get you from the basics to a real Google Apps Script pro.

Got a more specific problem you need help with, but don’t have the time to develop the skills? I can connect you with a trusted freelancer. I have a team of hand-picked experts ready to help.

*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.

~Yagi

Leave a Reply