Indian Standard Time and GMT from Local Time Using JavaScript

Posted by Sree Pillai on August 2nd, 2005 under category Internet
Topics: , , ,

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.

/***
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.

Email This · Share · Bookmark |Subscribe Feed|Subscribe Email

9 Responses to “Indian Standard Time and GMT from Local Time Using JavaScript”

  1. ankur says:

    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.

  2. anupam kumar singh says:

    i want to know the GMT difference between arunachal pradesh(india) & gujarat(india)

  3. Kaushik Ganguly says:

    But this code is expecting that the client side time is correct. So be careful while using the code.

  4. Uzair says:

    Hi Guys,
    I really appreciate your approach like this. Keep on this service as long as possible.

  5. Debasish Dasgupta says:

    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.

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

  7. Ravi says:

    I want to know difference between two times

  8. Hieu Nguyen says:

    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 :wink:
    Again, thank you very much

  9. Anonymous says:

    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

All comments are moderated, and spam comments are automatically deleted.

Article URI: http://teck.in/indian-standard-time-and-gmt-from.html
Author Email: sree@teck.in