Results 1 to 2 of 2

Thread: JavaScript - Date Calculation?

  1. #1
    Join Date
    Aug 2004
    Posts
    34

    JavaScript - Date Calculation?

    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.

    Code:
    /*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!

  2. #2
    Join Date
    Dec 2006
    Posts
    21
    a Good example is available at http://www.javascriptkit.com/javatut...fference.shtml

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

Similar Threads

  1. element specific javascript
    By gumbo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 04 Dec 2005, 06:25 PM
  2. element specific javascript
    By gumbo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 04 Dec 2005, 06:23 PM

Posting Permissions

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