Help Display Javascript Seconds As Real Time

C++, C#, Java, PHP, ect...
Post Reply
Klown
Posts: 89
Joined: Tue Feb 22, 2011 5:59 am

Help Display Javascript Seconds As Real Time

Post by Klown »

Hi Guys:

As I mentioned in the topic below I am just learning Javascript. I have searched high and low for a solution to this problem.

I need a method for getting the current time, adding some seconds onto that time, then formatting that time in javascript as readable time.
The code below is part of a larger function which gets looped by set interval.

My hope is i can get this code to display: HH:MM:SS as the current server time + the time in seconds I add to it. Not the amount of hours, mins and seconds since a time in the past. this currently shows the real time if I remove the +travelTime from the variables but I have tried a lot of combinations to get this to work as well as it does so far.

Thanks in advance for any help you can offer!

Here is my code so far:

Code: Select all

var travelTime = (distance * timePerUnit) / travelSpeed;

var timeH = new Date().getHours()+travelTime;
var timeM = new Date().getMinutes()+travelTime;
var timeS = new Date().getSeconds()+travelTime;

//var arriveAt = (time + travelTime);

var arriveAt = timeH + ":" + timeM + ":" + timeS;

document.getElementById('arrivalTime').value = arriveAt;
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
User avatar
Renavoid
Posts: 60
Joined: Sat Feb 23, 2013 7:48 pm

Re: Help Display Javascript Seconds As Real Time

Post by Renavoid »

I recommend using some sort of plugin to format the date, though you don't have to do this. That would allow you to do something like date.toString('hh:mm:ss'), though it depends on the implementation you can find. Since you're trying to display one time, I suggest manipulating a single Date object, rather than multiple. Create a variable, and then you can use the setSeconds method to add your travel time - and it will correctly update minutes, etc.

Check out this article to help you get the server time, rather than the random computer's local time: http://www.techrepublic.com/article/con ... pt/6016329

example code:

Code: Select all

var travelTime = (distance * timePerUnit) / travelSpeed;

var time = new Date();
time.setSeconds(time.getSeconds() + travelTime); // set the seconds to the current seconds + your travel time

var timeH = time.getHours();
var timeM = time.getMinutes();
var timeS = time.getSeconds();
If you don't want to use a date format library, then you can do something like this, acknowledging that it's not a 12 based clock, but 24:

Code: Select all

var timeH = (time.getHours() <= 9 ? '0' : '') + time.getHours(); //creates a 2 digit hour string. i.e. 09 or 14
var timeM = (time.getMinutes() <= 9 ? '0' : '') + time.getMinutes(); 
var timeS = (time.getSeconds() <= 9 ? '0' : '') + time.getSeconds();

var arriveAt = timeH + ':' + timeM + ':' + timeS; //e.g. 09:13:06 as a string
The winds of change are blowing; will you heed their call?
Klown
Posts: 89
Joined: Tue Feb 22, 2011 5:59 am

Re: Help Display Javascript Seconds As Real Time

Post by Klown »

Thanks again for the valuable help with this. The setSeconds method works perfectly for my needs and the time displays as expected! If it were not for helpful people, such as those found on this site, it would be much more time consuming for hobbyists to learn new programming languages.

Thanks again!

Dustin
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
Post Reply

Return to “Coding”