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. 

Hire a Google Workspace Developer for your Business Needs

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.

🐐You can support me for free by using this Amazon affiliate link in your next tech purchase :Computers & Stuff! 🐐
Create and Publish Google Workspace Add-ons with Apps Script Course 300px

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.

Create and Publish a Google Workspace Add-on with Apps Script Course

Need help with Google Workspace development?

My team of experts can help you with all of your needs, from custom app development to integrations and security. We have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.

~Yagi

Leave a Reply