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.

11 Comments

  1. Anupam, you sounds like too stupid. Are you alien to India or from Mars?
    Both Arunachal pradesh and Gujrat are part of India and share common TimeZone i.e. +0530 GMT.

    Suggestion: Do some research before asking such stupid questions.

    • Mr. Anupam question is correct.

      it takes nearly 4mins to cross one longitute at equator. the diff between Arunachal and gujrat is about 30 longitudes. hence the rough calculation will be 120 mins. the correct calculation will be dependent on longitudes. travel northwards, the difference will decrease.

      when the local time at Guj is 6000hrs, the local time at Arunachal whould be 8300 hrs. if at Guj a person leaves office at 1800 hrs, it is acually 2000 hrs at arunachal pradesh.

      For every long time discussions are being help even in govt circles that india must have atlest two IST’s so that local time descrepency like this can be avoided; this will save money for govt also as locals will be more tuned to the local time and something simple like electicity can be saved.

  2. There is an interesting relation between IST and GMT. Hold a watch, showing IST, upside down and and the inverted watch will show the GMT and vice versa.

  3. in geography, we teach students about LOCAL AND STANDARD TIME and the difference between the two. we see them in relation to longitude,and tell them how to calculate time of other countries with the help of some formulas.but I am disappointed because you don’t have any such information.

  4. I was searching for a js code that can display local time without any server side scripting and I found this great piece of code.

    Thanks for sharing and keep up the great work buddy 😉
    Again, thank you very much

  5. The function you provided, getCurrentIST(), worked perfectly for me. My company has outsourced our Tech Support to Bangalore and I needed to show IST on our internal web page. Thanks!

    – Tim –

Leave a Reply

Your email address will not be published.


*