views:

82

answers:

4

I am trying to see if my JavaScript file's function is written correctly in order to display "Sunday, December 24, 2006". So here is the function:

function XmasDays(thisDate) {    

  var XMYear = thisDate.getFullYear();    
  var XMDay = new Date("December, 25, 2005"); 

  XMDay.setFullYear(XMYear);    

  var dayTotal = (XMDay-thisDate)/(1000*60*60*24);
  dayTotal = Math.round(dayTotal);   

  return dayTotal; 
}

I've tested the following:

var XMYear=thisDate.getFullYear();    
var XMDay=new Date("December, 25, 2005");  
XMDay.setFullYear(XMYear);

and they output the correct information to my web page but I can't seem to figure out if the following three are written correctly can you help me see if any of these are in error? Thanks!!

var dayTotal=(XMDay-thisDate)/(1000*60*60*24); 

dayTotal=Math.round(dayTotal);

return dayTotal;

If you have any questions please ask. Thanks so much for any and all help!

A: 

You might like Date.js.

Your problem above appears that you are subtracting two date objects. Get the appropriate unit from each object (days?) and subtract those.

See here

Upper Stage
Thanks for giving me the link to date.js I think that will help me a lot in the future! :)
Ashley
It's one of my essential libraries!
Upper Stage
A: 

You should first decide what you want the function to do: what kinds of input are valid for it, and what should be its output for a given input.

Then you should check if it really does that. Either by looking at the code or by giving it various arguments as input and seeing its output.

You said: 'I am trying to see if my JavaScript file's function is written correctly in order to display "Sunday, December 24, 2006"'. But you probably don't want the function to always do the same thing no matter what is the thisDate argument value. Also, the function doesn't display anything so it clearly doesn't do what you describe.

Amnon
A: 

Add an alert to your code, or write the output to the page before you return to see what the value is, if you haven't already.

Your code returns 16 for me for today's date, which is December 8th, 1:20 PM. That could be correct, depending on what you want. There are ~16.44 days until 12 Midnight on Christmas.

// Number of milliseconds from now until midnight Christmas
// divided by the number of milliseconds in a day, which
// gives the number of days until Christmas
var dayTotal=(XMDay-thisDate)/(1000*60*60*24); 

// This rounds the number to the closest whole number
// Rounding up might be more appropriate in this case
dayTotal=Math.round(dayTotal);

// Debug output
alert(dayTotal);

return dayTotal;

I would say that you probably want Math.ceil, to give you the number of days rounded up, because if you are 12 hours away from midnight on Christmas, I would say there is still one day left until you get to midnight.

But other than that, your code seems correct.

Jeff B
+1  A: 

I found the solution for making the following function work: function XmasDays(thisDate) { var XMYear=thisDate.getFullYear(); var XMDay=new Date("December, 25, 2005"); XMDay.setFullYear(XMYear); var dayTotal=(XMDay-thisDate)/(1000*60*60*24);
dayTotal=Math.round(dayTotal); return dayTotal; } All I needed to do was remove XMDay.setFullYear(XMYear); from the function because this was eliminating the previous data from the date and confusing it. Then I added .getTime() to this part of the function: var dayTotal=(XMDay-thisDate)/(1000*60*60*24); so that it looked like this: var dayTotal=(XMDay.getTime()-thisDate.getTime())/(1000*60*60*24); In order to change the date to get different results you would go to var XMDay=new Date("December 25, 2009"); and change the date within the parentheses. Doing all of the above made the function work properly and display the date and correct days until Christmas along with the corresponding comment properly. So my revised function looks like this: function XmasDays(thisDate) { var XMYear=thisDate.getFullYear(); var XMDay=new Date("December 25, 2009"); var dayTotal=(XMDay.getTime()-thisDate.getTime())/(1000*60*60*24); dayTotal=Math.round(dayTotal); return dayTotal; } Thanks to those who answered and gave me tips but I already figured it out. I appreciate it greatly though your input and help!

Ashley