How to Convert Javascript UTC Date to Eastern Time

Written by daag alemayehu
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert Javascript UTC Date to Eastern Time
(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.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 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. 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 local Offset = 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. 3

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

  4. 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. 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 bar Day = new Date(fooUTC + EAST)”). This Date object represents your original UTC date, now converted to Eastern Time.

Tips and warnings

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

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.