Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.01 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. javascript server time script giving me NaN NaN [closed]
  2. var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
  3.  
  4. function showLocalTime(container, servermode, offsetMinutes, displayversion){
  5.     if (!document.getElementById || !document.getElementById(container)) return
  6.     this.container=document.getElementById(container)
  7.     this.displayversion=displayversion
  8.     var servertimestring=(servermode=="server-php")? 'August 31, 2007 22:41:04' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
  9.     this.localtime=this.serverdate=new Date(servertimestring)
  10.     this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
  11.     this.updateTime()
  12.     this.updateContainer()
  13. }
  14.  
  15. showLocalTime.prototype.updateTime=function(){
  16.     var thisobj=this
  17.     this.localtime.setSeconds(this.localtime.getSeconds()+1)
  18.     setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
  19. }
  20.  
  21. showLocalTime.prototype.updateContainer=function(){
  22.     var thisobj=this
  23.     if (this.displayversion=="long")
  24.         this.container.innerHTML=this.localtime.toLocaleString()
  25.     else{
  26.         var hour=this.localtime.getHours()
  27.         var minutes=this.localtime.getMinutes()
  28.         var seconds=this.localtime.getSeconds()
  29.         var ampm=(hour>=12)? "PM" : "AM"
  30.         var dayofweek=weekdaystxt[this.localtime.getDay()]
  31.         this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
  32.     }
  33.     setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
  34. }
  35.  
  36. function formatField(num, isHour){
  37.     if (typeof isHour!="undefined"){ //if this is the hour field
  38.         var hour=(num>12)? num-12 : num
  39.         return (hour==0)? 12 : hour
  40.     }
  41.     return (num<=9)? "0"+num : num//if this is minute or sec field
  42. }
  43.        
  44. new showLocalTime("timecontainer", "server-ssi", 0, "short")
  45.        
  46. new showLocalTime("timecontainer", "server-php", 0, "short")