Link to home
Start Free TrialLog in
Avatar of srk1982
srk1982

asked on

How to get the locatime using javascript

Hi experts,

    I am using this js function for getting the local time according to country where the website is viewed.
   If the website is viewed in singapore it shud show the singapore local time.
   If the website is viewed in hongkong it shud show the Hongkong local time.

It is not working for me... OR is there any other way??

URGENT...Thanks.
// JScript File
var WindowObjectReference = null;
//Region Time Display Function
/*Time Display Function for all Website pages*/
    var worldtime = new Array()
    
    worldtime[0]="Hong Kong,8"
 
    var hours
    var shifthours
    var minutes
    var seconds
    var localhours
    function showTime()
    {
        for (i=0; i<=worldtime.length-1;i++) 
        {
		    thisplace=worldtime[i].split(",")
		    thistime= new Date()
		    hours=thistime.getUTCHours()
		    hours=eval(hours)
		    shifthours=eval(thisplace[1])
		    localhours=eval(shifthours+hours)
		    if (localhours <0) {localhours=24+localhours}
		    if (localhours >=24) {localhours=localhours-24}
    	     
		    minutes=thistime.getUTCMinutes()
		    seconds=thistime.getUTCSeconds()
    	
		    if (thisplace[0]=='Delhi') 
		    {
			    minutes=eval(minutes+30)
			    if (minutes>=60) {
				    minutes=eval(minutes-60)
				    localhours=eval(localhours+1)
			        }
		    }
		    if (eval(minutes) < 10) {minutes="0"+minutes}
		    if (eval(seconds) < 10) {seconds="0"+seconds}
		    if (eval(localhours) < 10) {localhours="0"+localhours}
    		    
 
		       
		        if(document.getElementById('hdate')!=null && thisplace[0]=='Hong Kong')
		            document.getElementById('hdate').innerText = thistime.toLocaleDateString()+ ' ' + localhours+":"+minutes;
        }
        
            setTimeout("showTime()",1000)
    }
    
    showTime();
//End Region Time Display Function
</script>

Open in new window

Avatar of Haris V
Haris V
Flag of India image


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

Avatar of srk1982
srk1982

ASKER

hi sirah,

      It shud be a generalised function.Let me make it simple.
I want to get the date time of client side computer and show it in my website using javascript.

URGENT,
thanks.
ASKER CERTIFIED SOLUTION
Avatar of srk1982
srk1982

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial