Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

difference between dates? 2

Status
Not open for further replies.

iLy54

Programmer
Sep 5, 2002
33
0
0
ZA
hi all,
im need to display the difference between 2 dates. but i want it 2 show as a count down with number of days, hours, minutes and seconds.
can anyone help me out on this one?
any help would be greatly appreciated.
thanx so much. -Mon3y is the r00t of all evil and every man needs roots-
 
<script>
var dteNow = new Date()
var dteFutureEvent = new Date(2003,0,12,14,33)
var dteDiff = new Date(0,0,0,0,0);
dteDiff = new Date(dteDiff.getTime() +(dteFutureEvent.getTime() - dteNow.getTime()))
var strMonths = (dteDiff.getMonth()+1).toString()
var strDays = (dteDiff.getDate()).toString()
var strHours = (dteDiff.getHours()).toString()
var strMinutes = (dteDiff.getMinutes()).toString()

alert(&quot;difference is: &quot; + strMonths + &quot; months, &quot; + strDays + &quot; days, &quot; + strHours + &quot; hours, and &quot; + strMinutes + &quot; minutes.&quot;);


</script>
 
<html>
<script>

function showTime(){
var newYear = new Date()
var now = new Date()

newYear.setMonth(0)
newYear.setDate(1)
newYear.setYear(2003)
newYear.setHours(0)
newYear.setMinutes(0)
newYear.setSeconds(0)

diff = newYear.getTime() - now.getTime()
days = Math.floor(diff / (1000 * 60 * 60 * 24))
hours = Math.floor((diff - (days * 1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
mins = Math.floor((diff - (days * 1000 * 60 * 60 * 24)- (hours * 1000 * 60 * 60)) / (1000 * 60))
secs = Math.floor((diff - (days * 1000 * 60 * 60 * 24)- (hours * 1000 * 60 * 60) - (mins * 1000 * 60)) / 1000)
document.frmTest.clock.value = days + &quot; - &quot; + hours + &quot; - &quot; + mins + &quot; - &quot; + secs


setTimeout(&quot;showTime()&quot;,1000)
}
</script>

<body onload=&quot;showTime()&quot;>
<form name=&quot;frmTest&quot;>
<input name=&quot;clock&quot;>
</form>
</html> -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
thanx harmmeijer. but i was thinkin something more along the lines of a digital clock like thing that counts backwards between 2dates. sory if i was vague with my question.
could i be helped with this?
thanx again. -Mon3y is the r00t of all evil and every man needs roots-
 
Sorry, my first script didn't work so good.
Here is the correct script you can call the function in a setTimeout.
Be aware that getYear() can give strange values for 19.. dates so you should set the inYear in a more save way.
Here is the function:

<script>
var dteNow = new Date();
var dteFutureEvent = new Date(dteNow.getTime()+10000000000);
function getDateDiff(dteLow,dteHigh) {
var intMonthDays = new Date(dteHigh.getYear(),dteHigh.getMonth());
var intMonthDays = intMonthDays.getTime()-1;
var intMonthDays = new Date(intMonthDays).getDate();

var lngDiff = dteHigh.getTime() - dteLow.getTime();
var intYear = dteHigh.getYear() - dteLow.getYear();
// getYear can have strange values for 19..

var intMonths = ((intYear * 12)+dteHigh.getMonth()) - dteLow.getMonth();
var intDays = dteHigh.getDate() - dteLow.getDate();
var intHours = dteHigh.getHours() - dteLow.getHours();
var intMinutes = dteHigh.getMinutes() - dteLow.getMinutes();
var intSeconds = dteHigh.getSeconds() - dteLow.getSeconds();

if(intSeconds<0){
intMinutes = intMinutes -1;
intSeconds = intSeconds + 60;
}

if(intMinutes<0) {
intHours = intHours -1;
intMinutes = intMinutes + 60;
}
if(intHours<0) {
intDays = intDays -1;
intHours = intHours + 24
}

if(intDays<0) {
intMonths = intMonths - 1;
intDays = intDays + intMonthDays;
}

//var strDays = (dteDiff.getDate()).toString()
//var strHours = (dteDiff.getHours()).toString()
//var strMinutes = (dteDiff.getMinutes()).toString()
return (intMonths.toString() + &quot; months, &quot; + intDays.toString() + &quot; days, &quot; + intHours.toString() + &quot; hours, &quot; + intMinutes.toString() + &quot; minutes and &quot; + intSeconds.toString() + &quot; seconds.&quot;);
}
alert(getDateDiff(new Date(),new Date(2003,1,1)));
</script>
 
thanx, both your scripts were extremely helpful to me.
but if i could ask 1 mor thing?
when it displays single digits like 9 secs how could i change it two show to digits like 09 secs? -Mon3y is the r00t of all evil and every man needs roots-
 
you can pad any number like so :

var numberNine = 9; // this can be any variable and any value
numberNine = (numberNine < 10) ? &quot;0&quot; + numberNine : numberNine;

I hope this helps. Gary Haran
 
i dont get how i could use it in the code above? like the count down in the input field that mwolf00 supplyd.it continues to count down. could i say:

if (secs<10)
&quot;0&quot; + secs: secs

i hop im makin any sort of sense hear. -Mon3y is the r00t of all evil and every man needs roots-
 
The following code got the year thing fixed and displays hours and minutes with a 0?.


<script>
var dteNow = new Date();
var dteFutureEvent = new Date(dteNow.getTime()+10000000000);
function getDateDiff(dteLow,dteHigh) {
var intMonthDays = new Date(dteHigh.getYear(),dteHigh.getMonth());
var intMonthDays = intMonthDays.getTime()-1;
var intMonthDays = new Date(intMonthDays).getDate();

var lngDiff = dteHigh.getTime() - dteLow.getTime();
var intLowYear = dteLow.getYear();
var intHighYear = dteHigh.getYear();

if(intLowYear < 10){ // IE and Mozilla remove the 19 for 19.. dates put this back
intLowYear = parseInt((&quot;190&quot; + intLowYear.toString()));
}else if(intLowYear<100){
intLowYear = parseInt((&quot;19&quot; + intLowYear.toString()));
}else if(intLowYear < 200){ // Mozilla will give 101 for 2001 year date and 102 for 2002 ...
intLowYear = parseInt(&quot;20&quot; + intLowYear.toString().substring(1,3));
}

if(intHighYear < 10){ // IE and Mozilla remove the 19 for 19.. dates put this back
intHighYear = parseInt((&quot;190&quot; + intHighYear.toString()));
}else if(intHighYear<100){
intHighYear = parseInt((&quot;19&quot; + intHighYear.toString()));
}else if(intHighYear < 200){ // Mozilla will give 101 for 2001 year date and 102 for 2002 ...
intHighYear = parseInt(&quot;20&quot; + intHighYear.toString().substring(1,3));
}

var intYear = intHighYear - intLowYear;
// getYear can have strange values for 19..
var intMonths = ((intYear * 12)+dteHigh.getMonth()) - dteLow.getMonth();
var intDays = dteHigh.getDate() - dteLow.getDate();
var intHours = dteHigh.getHours() - dteLow.getHours();
var intMinutes = dteHigh.getMinutes() - dteLow.getMinutes();
var intSeconds = dteHigh.getSeconds() - dteLow.getSeconds();

if(intSeconds<0){
intMinutes = intMinutes -1;
intSeconds = intSeconds + 60;
}

if(intMinutes<0) {
intHours = intHours -1;
intMinutes = intMinutes + 60;
}
if(intHours<0) {
intDays = intDays -1;
intHours = intHours + 24
}

if(intDays<0) {
intMonths = intMonths - 1;
intDays = intDays + intMonthDays;
}
intSeconds = (intSeconds < 10)? (&quot;0&quot; + intSeconds.toString()): intSeconds.toString();
intMinutes = (intMinutes < 10)? (&quot;0&quot; + intMinutes.toString()): intMinutes.toString;

return (intMonths.toString() + &quot; months, &quot; + intDays.toString() + &quot; days, &quot; + intHours.toString() + &quot; hours, &quot; + intMinutes + &quot; minutes and &quot; + intSeconds + &quot; seconds.&quot;);
}
alert(getDateDiff(new Date(1901,2,21),new Date(1902,1,1)));
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top