Indian Standard Time and GMT from Local Time Using JavaScript

If you are looking for current accurate Indian Standard Time or time in any country or timezone, check out the World Time page in ananthapuri.com.

I wanted to display Indian Standard Time in Ananthapuri.com irrespective of where the visitor is browsing from. The visitor could be from any timezone in the US, or India or UAE or UK or anywhere in the world. But the time shown in Ananthapuri.com should be IST.

Written below is the function I wrote in order to get IST. Needless to say, the IST displayed will be just as accurate as accuracy of host computer.

[advt]

/***
Get Indian Standard Time from visitor's local computer time.
Add getTimezoneOffset to get GMT/UTC
Add +330 minutes (IST is +5.5 hrs ahead of GMT) to get IST
***/

function getCurrentIST(){
  var dte = new Date();
  dte.setTime(dte.getTime() +
    (dte.getTimezoneOffset()+330)*60*1000);
  document.write(dte.toLocaleString());
}

The function written below, converts GMT time string into visitor’s local time, takes care of Daylight Savings too.

/***
Get visitor's Local Time from from GMT time string
sTime : Input date/time/timestamp format string
Any string parsable by Date.parse()
static method is accepted as input.
***/

function getLocalTimeFromGMT(sTime){
  var dte = new Date(sTime);
  dte.setTime(dte.getTime()
    - dte.getTimezoneOffset()*60*1000);
  document.write(dte.toLocaleString());
}

I used these functions to display India Time and news update time in News Headlines of Ananthapuri.com.

getTime()

Returns the numeric value corresponding to the time for the specified date according to local time from the visitor’s computer. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00.

getTimezoneOffset()

Returns the time-zone offset in minutes for the current locale, including Daylight savings time, from visitor’s computer. The time-zone offset is the difference between local time and Greenwich Mean Time (GMT).

toLocaleString()

Converts a date to a string, using the current locale’s conventions. Different platforms might assemble the input string in different ways depending on the user agent (browser) and operating system settings.

Be the first to comment

Leave a Reply