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.
In that case we need to get UTC date and add the difference minutes to it before display. For example if your database server is in India then you need to add 330 minutes (5 hours 30 minutes) to UTC time to get the correct database time in the client machine.Because the conversion in JSON will automatically happen internally
based on client machine's time zone.
Whenever we changed TIMEZONE date will be changed based on TIME ZONE (UTC OFFSET) but we do not need to consider time at all we need to display on DATE which is existed in our DATABASE use the below FUNCTION based on your requirement.
/***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.
Is it work for EST time zone also ? without using any string operation ?
ReplyDelete