PDA

View Full Version : JavaScript - Date Calculation?



web88js
25 Nov 2006, 12:37 PM
hello everyone:

I have an question regarding calculation involve dates.

the problem is like this. Show final examdate is December 20, 2006. Check the current date against
the final date.
a) if it is not the final exam date yet show how many days left before final
b) if it is final exam date, print a message to say it is final day.


my approach is initialize final date in string format, then convert current date to string format.
do calculation to see if final date has been reached.


/*variable initialization for current date */
var curDate = new Date();
var curDay = curDate.getDay();
var monthDay = curDate.getDate();
var curMonth = curDate.getMonth() + 1;
var curYear = curDate.getFullYear();

/* variable initialization for last day of class */
var finalDay = new Date ("December 13, 2006");
var lastDay = finalDay.getDate();
var lastMonth = finalDay.getMonth() + 1;
var lastYear = finalDay.getFullYear();

var month = new Array ("", "Jan", "Feb","Mar","Apr","May","Jun", "Jul", "Aug",
"Sep","Oct","November","December");
var temp = (month[curMonth] + " "+ monthDay +", " + curYear);


//convert current date to string format
var temp = (month[curMonth] + " "+ monthDay +", " + curYear);

//calculation that converts time to days
var temp1 = (finalDay - temp) / (1000 * 60 * 60 * 24);

alert (temp1);

the output is NaN. I think probably value of temp variable doesn't math with finalDay variable.
How can I fix this problem, so I can display days as a whole number.

Please give me some suggestions. thanks!

mrizwan
30 Dec 2006, 04:10 PM
a Good example is available at http://www.javascriptkit.com/javatutors/datedifference.shtml

--
Riz ...
... Free HTML 2 PDF conversion @ http://www.PDFo***y.com