Posted by: prajapatinilesh | November 22, 2007

Indian Standard Time and GMT from Local Time Using JavaScript

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());
}
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.


Advertisements

Responses

  1. good sign

  2. get indian time in php

    $date = date_default_timezone_set(‘Asia/Kolkata’);
    $today = date(“F j, Y, g:i a T”);

  3. I want change the current time 5 minutes ahead.How is this possible? is there any way please send it my mail id

  4. […] it? So how to track the hour of visit for the timezone you want? Thanks to Nilesh for his blog post on getting the time in particular timezone using JavaScript. Here is the code for […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: