Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date and time script help

    I have the following code on my site.

    I use this in the header of mysite

    Code:
    <script type="text/javascript">
    
    var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
    var montharray=("January","February","March","April","May","June","July","August","September","October","November","December")
    function showLocalgetthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    
    
    
    function showLocalTime(container, servermode, offsetMinutes, displayversion){
    if (!document.getElementById || !document.getElementById(container)) return
    this.container=document.getElementById(container)
    this.displayversion=displayversion
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
    this.localtime=this.serverdate=new Date(servertimestring)
    this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
    this.updateTime()
    this.updateContainer()
    }
    
    showLocalTime.prototype.updateTime=function(){
    var thisobj=this
    this.localtime.setSeconds(this.localtime.getSeconds()+1)
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
    }
    
    showLocalTime.prototype.updateContainer=function(){
    var thisobj=this
    if (this.displayversion=="long")
    this.container.innerHTML=this.localtime.toLocaleString()
    else{
    var hour=this.localtime.getHours()
    var minutes=this.localtime.getMinutes()
    var seconds=this.localtime.getSeconds()
    var ampm=(hour>=12)? "PM" : "AM"
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+ "
    "+montharray(month)+" ("+dayofweek+")"
    }
    setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
    }
    
    function formatField(num, isHour){
    
    
    </script>
    I use this to display the information.

    Code:
    <script type="text/javascript">
    new showLocalTime("timecontainer", "server-php", 0, "short")
    new showLocalTime("timecontainer2", "server-php", +300, "short")
    new showLocalTime("timecontainer3", "server-php", +360, "short")
    new showLocalTime("timecontainer4", "server-php", +600, "short")
    </script>
    This gives me 4 clocks which i set at different time zones

    Can someone assist me in showing me a way to accelerate the time for this clock. I would like the minutes to be only 5 seconds long versus the normal 60 seconds per minute. I fly a flight sim and the in game world clock is accelerated at this pace and my squad would like to be able to see the time it would currently be in the game so we can avoid launching the game at certain in game times. If this can't be done, does anyone know of a separate java script that can accomplish this? Thank you in advance, any and all help would be greatly appreciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    Well, this code is bad, anyways:
    Code:
    showLocalTime.prototype.updateTime=function(){
    var thisobj=this
    this.localtime.setSeconds(this.localtime.getSeconds()+1)
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
    }
    Eventually, this will lead to "clock creep". After all, it tekes some time for the code for setSeconds and the code for even the setTimeout to execute. Let's say it takes 10 milliseconds. So after 100 or so real seconds, you would have used 1000 milliseconds doing the processing and your clock is now off by one second!

    You should *NOT* increment your time by using getSeconds()+1.

    You should always go get the time via new Date() so that even if something "hangs" the JavaScript code for an indeterminate period, the next time the "tick" of the setTimeout (which probably REALLY should be setInterval) occurs, it will be self correcting.

    SO... I simply wouldn't code this at all the way you have.

    Instead, I would save the start time in a variable *without* accounting for the offset.

    Then each time the timeout (or interval) hits the code, I'd go get the current time, subtract off the start time to get the number of elapsed seconds, multiply that by 12 (5 seconds == 1 fast-clock minute, 60 / 5 ==>> 12), add *that* to the start time + offset (which I guess could indeed also be a stored variable) to get the fast-clock display time.

    Can you write the code from that description? If not, I'll take a stab at it.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,799
    Thanks
    78
    Thanked 4,410 Times in 4,375 Posts
    Oh, w.t.h.

    Wanted to play with this, myself.

    Works. Doesn't allow getting start time from server (to simplify...so I didn't have to have server code) or allow shoice of format (which seems overkill to me...why would you want different formats on the same web page??). But concept works like a charm:
    Code:
    <html><head>
    <script type="text/javascript">
    
    function FastClock( where, offset )
    {
        var c = document.getElementById(where);
        if ( c == null )
        {
            alert("Can not find '" + where + "' for FastClock");
            return;
        }
        this.container = c;
        this.startTime = ( new Date() ).getTime(); // you could use PHP to set server time instead
        this.offsetStart = this.startTime + offset * 60000;
        this.timer = null;
    
    }
    FastClock.prototype.updateTime = function()
    {
        var thisobj = this; 
        var now = new Date();
        var elapsed = now.getTime() - this.startTime;
        now.setTime( this.offsetStart + elapsed * 12 ); // using 12x fast clock
        this.container.innerHTML = now.toLocaleString();
        
        if ( this.timer == null ) 
        {
            this.timer = setInterval( function(){thisobj.updateTime()}, 1000); 
        }
    }
    
    function clocks( )
    {
        ( new FastClock("PDT", 0) ).updateTime();
        ( new FastClock("EDT", -180) ).updateTime();
        ( new FastClock("INDIA", -750) ).updateTime();
    }
    </script>
    </head>
    <body onload="clocks();">
    Local time: <span id="PDT"></span>
    <hr/>
    3 hours earlier: <span id="EDT"></span>
    <hr/>
    12.5 hours ago: <span id="INDIA"></span>
    </body>
    </html>

  • #4
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    salvo:

    Or, try it this way. Copy the code, then save it as an .html document.


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>None</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function init(){
    
    		var elapsedTimeCaption = "Flight Time: ";  // description of the elapsed time;
    		var timerDisplay = "";
    		var nDiv = document.getElementsByTagName('div');
    		for (i=0; i<nDiv.length; i++)
    			{
    			 if (nDiv[i].className = "gameTimer")
    				{
    				 timerDisplay = nDiv[i];
    				}
    			}		
    		var timerStep = 1000 * (5 / 60).toFixed(3) * .9425;     
    		timerDisplay.innerHTML = "00:00:00";		
    		var nSec = 0;
    		var nMin = 0;
    		var nHour = 0;		
    		var nTick = 0;
    		var nTime = new Date();
    		nTime.setHours(0,0,0,0)
    		setInterval(function()
    			{		
    			 nTick++;	
    			 if (nTick == 60)
    				{	
    				 nTick = 0;
    			 	 nTime =  new Date(nTime.setHours(nTime.getHours(),nTime.getMinutes()+1,nTime.getSeconds()));
    			 	 nSec = nTime.getSeconds();
    			 	 nMin = nTime.getMinutes();
    			 	 nHour = nTime.getHours();							 
    				}
    			 else	{				 
    				 nSec = nTick;
    				 nMin = nTime.getMinutes();
    			 	 nHour = nTime.getHours();
    				}
    			 nSec = nSec < 10 ? "0" + nSec : nSec;
    			 nMin = nMin < 10 ? "0" + nMin : nMin;
    			 nHour = nHour < 10 ? "0" + nHour : nHour;			
    			 timerDisplay.innerHTML = elapsedTimeCaption + nHour + ":" + nMin + ":" + nSec;
    			} , timerStep )
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
    
    </script>
    <style type="text/css">
    
    	 body {background-color: #fffacd; margin-top: 60px;}
    	.gameTimer {width: 300px; font-family: tahoma; font-size: 14pt; padding:5px;
    		    border: 1px solid black; margin-left: auto; margin-right: auto;
    		    margin-top: 15px; margin-bottom: 15px; text-align: center; background-color: #f0fff0;}
    
    </style>
    </head>
    	<body>
    		<div class="gameTimer"></div>
    	</body>
    </html>

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks old, this does work like a charm and I can use it for another project i'm working, so it's not a total loss. Thank you so much.

    I still have a problem, The ingame clock is not based off any time zone or server, it has its own internal clock so I would need to be able to set the initial time to synch it with that of the ingame time.

    Works. Doesn't allow getting start time from server (to simplify...so I didn't have to have server code) or allow shoice of format (which seems overkill to me...why would you want different formats on the same web page??). But concept works like a charm:
    The reason I wanted to set up the format is so that at anytime i could put it in any part of the site with any of the elements i wanted. Maybe in a spot with just the time or just the day etc...

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    12 pack thank you for trying to help but i'm not looking for a timer, I'm looking for an accelerated time/clock script. Bascially what old has showed me, but something I can set the intial time to reflect the in game time, each time game server is reset the time is as well and then I could just reset that in the script again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •