How to Convert Javascript UTC Date to Eastern Time

by Daag Alemayehu

People often express time zones from around the world as positive or negative offsets from Coordinated Universal Time, or UTC. UTC is the time standard based on International Atomic Time and in casual use is considered equivalent to Greenwich Mean Time (GMT). Using the JavaScript scripting language, you can easily convert UTC time to Eastern Time.

1

Instantiate a JavaScript Date object by calling the Date() constructor, passing to it as its sole parameter the UTC() method (e.g., for midnight on January 1, 2000 UTC time, “var fooDay = new Date(Date.UTC(2000,0,1,0,0,0,0))”). UTC() is a static method that returns the number of milliseconds that have elapsed since midnight of January 1, 1970 UTC time. Its syntax is Date.UTC(year, month, day, hours, minutes, seconds, milliseconds), where all arguments are expressed as digits. The first three arguments are required while the remaining arguments are optional. This Date object represents the time you want to convert, expressed in your local time.

2

Calculate the time difference in milliseconds between UTC time and your local time and store the result in a variable. To calculate your local offset, call the getTimezoneOffset() method of your Date object and multiply its return value by 60,000 (e.g., “var localOffset = fooDay.getTimezoneOffset() * 60000”). You multiply by 60,000 to convert minutes into milliseconds (1,000 milliseconds in one second * 60 seconds in one minute = 60,000 milliseconds).

3

Obtain the UTC value in milliseconds of your Date object by calling its getTime() method and adding the return value to your local offset (e.g., “var fooUTC = fooDay.getTime() + localOffset”).

4

Create a constant to represent the Eastern Time Zone offset in milliseconds (e.g., “const EAST = 3600000 * -5”). You multiply by 3,600,000 to convert hours into milliseconds (1,000 milliseconds in one second * 60 seconds in one minute * 60 minutes in one hour = 3,600,000 milliseconds), and you multiply by -5 because Eastern Time is five hours behind UTC time.

5

Instantiate another Date object by calling the Date() constructor, passing to it as its sole parameter the sum of the UTC value of the first Date object and your Eastern Time Zone offset constant (e.g., “var barDay = new Date(fooUTC + EAST)”). This Date object represents your original UTC date, now converted to Eastern Time.

Tip

  • check In JavaScript, the numerical representation for months ranges from 0 to 11 rather than from 1 to 12. So, for example, January would be “0,” February “1”, March “2,” and so on.

About the Author

Daag Alemayehu is a professional Web developer and a Georgia-licensed attorney. He holds a Bachelor of Science degree in computer science from Georgia Tech and a Juris Doctor from the Washington University in St. Louis Law School. He has been blogging and writing on the Internet for sites such as eHow for eight years.

Photo Credits

  • photo_camera Ciaran Griffin/Lifesize/Getty Images