Wrox Home  
Search P2P Archive for: Go

  Return to Index  

beginning_javascript thread: How do I calculate the days left to a date in future?


Message #1 by lee.moorley@c... on Sat, 25 May 2002 14:42:43
I have found a script that will calculate the days left in the current 
month.  What I need is the days countdown until a specific date in the 
future.
ie  "96 days until Xmas" which counts down the days as the date approaches.
Can anyone help?
Message #2 by "TurnenT" <turnent@c...> on Sat, 25 May 2002 12:34:08 -0700
There is a great website that has a script that counts down to a date you
enter - but no seconds or milliseconds.

It will say something like

1 year
2 months and
3 days left until a certain date.

Is that what you want?

Heres the link

http://www.javascript-page.com/makecount.html

Message #3 by "Lee Moorley" <lee.moorley@c...> on Tue, 28 May 2002 08:56:53 +0100
Thank you for your link for days countdown.  Almost what I need.  I want to
say something like "Only 61 days left until 'so and so' ".  I do not need to
see any forms as it will spoil the look on the page.  So I am still looking!

----- Original Message -----
From: "TurnenT" <turnent@c...>
To: "Beginning JavaScript" <beginning_javascript@p...>
Sent: 25 May 2002 20:34
Subject: [beginning_javascript] Re: How do I calculate the days left to a
date in future?


> There is a great website that has a script that counts down to a date you
> enter - but no seconds or milliseconds.
>
> It will say something like
>
> 1 year
> 2 months and
> 3 days left until a certain date.
>
> Is that what you want?
>
> Heres the link
>
> http://www.javascript-page.com/makecount.html
>
>
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> r-20
> Constructing Accessible Web Sites
> http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> r-20
> Practical JavaScript for the Usable Web
> http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> r-20
>
>

Message #4 by "Greg Dunn" <greg.dunn@n...> on Tue, 28 May 2002 10:50:16 -0500
So change the script to hard code the date rather than getting it from the
form...

Greg



-----Original Message-----
From: Lee Moorley [mailto:lee.moorley@c...]
Sent: Tuesday, May 28, 2002 2:57 AM
To: Beginning JavaScript
Subject: [beginning_javascript] Re: How do I calculate the days left to
a date in future?


Thank you for your link for days countdown.  Almost what I need.  I want to
say something like "Only 61 days left until 'so and so' ".  I do not need to
see any forms as it will spoil the look on the page.  So I am still looking!

----- Original Message -----
From: "TurnenT" <turnent@c...>
To: "Beginning JavaScript" <beginning_javascript@p...>
Sent: 25 May 2002 20:34
Subject: [beginning_javascript] Re: How do I calculate the days left to a
date in future?


> There is a great website that has a script that counts down to a date you
> enter - but no seconds or milliseconds.
>
> It will say something like
>
> 1 year
> 2 months and
> 3 days left until a certain date.
>
> Is that what you want?
>
> Heres the link
>
> http://www.javascript-page.com/makecount.html
>
>
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> r-20
> Constructing Accessible Web Sites
> http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> r-20
> Practical JavaScript for the Usable Web
> http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> r-20
>
>



---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20


Message #5 by "TurnenT" <turnent@c...> on Tue, 28 May 2002 15:32:38 -0700
You can hide the look of the forms if you want with a stylesheet.


Or, Here is the code, I haven't tested it yet....
<SCRIPT LANGUAGE="JavaScript">
// <!--
var now = new Date();
var curDay = now.getDay();
var curMonth = now.getMonth();
var curYear =  "20"+ now.getYear();
parseInt(curYear);

var daysLeft = calcDays(curDay,curMonth,curYear);
alert(daysLeft);

function calcDays(day,month,year)
{
var monthTotal;
var dayCount;
var X

var monthDays = new Array();
monthDays[0] = 31;
monthDays[1] = 28;
monthDays[2] = 31;
monthDays[3] = 30;
monthDays[4] = 31;
monthDays[5] = 30;
monthDays[6] = 31;
monthDays[7] = 31;
monthDays[8] = 30;
monthDays[9] = 31;
monthDays[10] = 30;
monthDays[11] = 31;
if (year % 4 == 0) { monthDays[2] = 29; } // leap year fix

  for (i = curMonth; i < 12; i++)
  {
   monthTotal = monthTotal + monthDays[i];
  }

dayCount = (X - Day) + monthTotal;
return dayCount;
}
// -->
</SCRIPT>




Message #6 by "TurnenT" <turnent@c...> on Tue, 28 May 2002 15:41:13 -0700
Sorry, I accidentally sent it...
Haven't tested it yet...
<SCRIPT LANGUAGE="JavaScript">
// <!--
function calcDays(X,day,month,year)
{
var monthDays = new Array();
monthDays[0] = 31;
monthDays[1] = 28;
monthDays[2] = 31;
monthDays[3] = 30;
monthDays[4] = 31;
monthDays[5] = 30;
monthDays[6] = 31;
monthDays[7] = 31;
monthDays[8] = 30;
monthDays[9] = 31;
monthDays[10] = 30;
monthDays[11] = 31;
if (year % 4 == 0) { monthDays[2] = 29; } // leap year fix
var monthTotal;
  for (i = month; i < 12; i++)
  {
   monthTotal = monthTotal + monthDays[i];
  }
var dayCount = (X - Day) + monthTotal;      //X is the date...EG, if you
wanted to count down to ground hogs day, it would
return dayCount;                                         // be 2... don't
forget to change the date below (variable now);
}



var now = new Date(); // Count down, EG Christmas :  "December, 28, 2002"
<------ change this
var curDay = now.getDay();
var curMonth = now.getMonth();
var curYear =  now.getYear();
var daysLeft = calcDays(curDay,curMonth,curYear);
document.write(daysLeft);
// -->
</SCRIPT>






  Return to Index