Hello. Today is:

And the time is now (according to your computer):

So, according to the date and time on this computer, there are days to Christmas!

The additional code needed is shown below.

myDate=new Date(); cmas=Date.parse("Dec 25, "+myDate.getFullYear()) today=Date.parse(myDate) daysToChristmas=Math.round((cmas-today)/(1000*60*60*24)) //daysToChristmas=10 document.write("<b>"); if (daysToChristmas==0) document.write("Today is Christmas ... Merry Christmas!") if (daysToChristmas<0) document.write("<br>Christmas was "+-1*(daysToChristmas)+" days ago."); if (daysToChristmas>0) document.write("<br>There are "+daysToChristmas+" days to Christmas!") document.write("</b>") |

Date.parse gives us a value (for a particular date) of the number of milliseconds since 1st January 1970. It converts the date into a number. So

Date.parse("Dec 25, 2000")

gives us the number milliseconds since 1970.

and ...

Date.parse(myDate)

gives us the number of milliseconds since 1/1/70 when you opened this page. This is, as you know milliseconds.

So, after getting these values, all that remains is to do appropriate sums to turn these values into the days to Christmas. We convert these values into days and then subtract one from the other.

daysToChristmas=

Math.round((cmas-today)/(1000*60*60*24))

To get a whole number, we use the Math object. In this case, we use **Math.round**.
This rounds the number up or down to the nearest integer. (**parseInt**,
would give us the whole number part only).

We take into account the 3 possibilities which are that

- today is before Christmas,
- today is after Christmas and
- today is Christmas.

We handle these with the three if-statements.

if (daysToChristmas==0)

document.write("Today is Christmas ... Merry Christmas!")

if (daysToChristmas<0)

document.write("<br>Christmas was "+-1*(daysToChristmas)+" days ago.");

if (daysToChristmas>0)

document.write("<br>There are "+daysToChristmas+" days to Christmas!")

So we get the right (or intended) message whatever the day.

There are two things to note about the code:

document.write("<br>Christmas was "+

-1*(daysToChristmas)+" days ago.");

Because a time after Christmas will be negative, and because we don't want to show negative numbers in our code, we multiple the value by -1 to make it a positive number. (We could have used Math.abs() if we wished, and this would have been a better choice.)

document.write("<br>Christmas was "+

Math.abs(daysToChristmas)+" days ago.");

The second thing to note is:

//daysToChristmas=10

This is nothing at all to do with the code, but was used together with commenting out the code for calculating daysToChristmas and just checking we would get the results we intended.