i'm creating site neighbor has christmas light show.
the show runs every year 6 december till 1 january twice evening: @ 6.30pm , @ 8.00pm.
we want add countdown on website says:
next show: 00:00:00 (hh:mm:ss)
but how do that. when search on web every 1 says have use api countdown.
but use 1 date count down to, think have write 1 myself in javascript.
can that?
i guess have use many if
/else
statements, starting "is month 1, 12 or else?", followed "has yet been 18.30?" (i want 24-hours) , "has been 20.00" , on.
but there better way, because seems lot of work me.
javascript has built-in date object makes dealing dates , times bit less manual:
if supply no arguments constructor, it'll give current date (according end user's computer):
var = new date();
you can set specific date supplying year, month (zero-indexed january), day, , optionally hour, minute , second:
var = new date(); var first_show = new date(now.getfullyear(), 11, 6, 18, 30);
you can use greater- , less-than comparisons on these date objects check whether date after or before another:
var = new date(); var first_show = new date(now.getfullyear(), 11, 6, 18, 30); alert(now < first_show);// alerts true (at date of writing)
so, could:
- create date objects current date, , each show year (and 1st jan shows next year)
- loop through show dates in chronological order, and
- use first 1 that's greater current date basis countdown.
note: should use server-side set now
accurate parameters, instead of relying on new date()
, because if end-user's computer set wrong time, it'll give wrong result.
Comments
Post a Comment