Utility Class to change local time in GMT and GMT time in Local

Utility Class to change local time in GMT and GMT time in Local

Again I have come up with another interesting JavaScript utility. I was assigned this task to convert local time in GTM time, GMT time in Local Time and then format the date into dd/mm/yyyy hh:mmAM/PM format. After a good two hour googling and haunting, I came to create this utility class:

function localTime()
{
this.GMTTime=function(date)
{
var utc=date.getTime() + date.getTimezoneOffset()*60000;
var gmtDate=new Date(utc);
return gmtDate.toLocaleString();
}
this.LocalTime=function(date)
{
var utc=date.getTime() – date.getTimezoneOffset()*60000;
var gmtDate=new Date(utc);
return gmtDate.toLocaleString();
}
this.toString=function(time)
{
var Month=time.getMonth();
var Date=time.getDate();
var Year=time.getFullYear();
var Hour=time.getHours();
var Minute=time.getMinutes();
var AmPm;

if(Hour < ampm="AM" hour="GMTHour;">
else{ AmPm=”PM”; Hour=Hour-12; }

if(Month < month="0">
if(Date < date="0">
if(Hour < hour="0">
if(Minute < minute="0">

var timeString=Month+”/”+Date+”/”+Year+” “+Hour+”:”+Minute+””+AmPm;
return timeString;
}

}

This function can be called from any where like this:

var curTime=new Date();
var lTime=new localTime();
var gmtTime=new Date(lTime.GMTTime(curTime));
var localTime=new Date(lTime.LocalTime(gmtTime));
document.write(“GMT Time: “+lTime.toString(gmtTime));
document.write(“
“);

document.write(“Loca Time: “+lTime.toString(localTime));

Leave a Reply

Your email address will not be published. Required fields are marked *