How to Convert Javascript UTC Date to Eastern Time

By Daag Alemayehu

Finding a key does not require editing the Windows Registry.
i Ciaran Griffin/Lifesize/Getty Images

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.

Step 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.

Step 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).

Step 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”).

Step 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.

Step 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.

×