JRS: In article <06*****************************@REMOVE.uml.edu> , dated
Tue, 7 Sep 2004 02:33:06, seen in news:comp.lang.javascript, Kevin
Duggan <ke**********@REMOVE.uml.edu> posted :
I can not understand how to get this so the page
reads "Only 9 days till Thanksgiving"
or whatever the date may be. (Xmass ect....)
ect?
var holidayCntMsg = "Leave the party to KEVSBLues Catering"
var curDay = new Date()
var tMonth = curDay.getMonth()
function HolidayDays() {
switch (tMonth) {
case 2:
var tHoliday = new Date("March 17, 2001")
var curHoliday = tHoliday.getTime()-curDay.getTime()
if (curHoliday > 0) {
curHoliday = Math.ceil(curHoliday /
(1000*60*60*24))
holidayCntMsg = "Only " +curHoliday+ " days
until St. Patrick's Day!"
}
break
case 11:
var tHoliday = new Date("December 25, 2001")
var curHoliday = tHoliday.getTime()-curDay.getTime()
if (curHoliday > 0) {
curHoliday = Math.ceil(curHoliday /
(1000*60*60*24))
holidayCntMsg = "Only " +curHoliday+ " days til
CHRISTMAS!"
}
break
}
return holidayCntMsg
}
Code posted to News should not be machine-wrapped. The author should
deal with that, rather than forcing it on his possible readers, who
generally will not want to bother.
In News, an 8-space tab is too much indentation.
Your code is repetitive; use a single function, and call it for each
case.
You need to get the year right, and to calculate the November date
accordingly.
Your day-count will change at midnight, or an hour earlier or later,
depending on the state of Summer Time at the user's location on the
current and cited date; that's inelegant. Code containing 1000*60*60*24
is always suspect, though not necessarily wrong. You can do setHours(0)
on today's date, then Math.round() on the difference (unchecked).
Given that you only show a holiday (and St.P is not AFAIK a holiday
anywhere that celebrates 04-07) once its month is started, it would be
simpler just to subtract the day-of-month.
What's UML? Lansing??
Read the newsgroup FAQ before posting, and you will often be led to the
methods you need.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.