Reputation: 825
I'm using this plugin to do a countdown. In his example, he is counting down to Australia Day
$(function () {
var countdownDate = new Date();
countdownDate = new Date(countdownDate.getFullYear() + 1, 1 - 1, 26);
$('#countdown').countdown({until: countdownDate});
$('#year').text(countdownDate.getFullYear());
});
I need it to count down to 8 PM EST on Tuesday, November 6, 2012, but I'm not sure how to customize it. The Date() function confuses me.
Upvotes: 0
Views: 945
Reputation: 1520
These lines determine the date that it's counting down to:
var countdownDate = new Date();
countdownDate = new Date(countdownDate.getFullYear() + 1, 1 - 1, 26);
The first line here creates a new JavaScript Date Object. It defaults to right now when you create a Date.
The next line sets the date to a specific point in time using the structure:
new Date(year, month, day, hours, minutes, seconds, milliseconds);
I'll go one step at a time:
1. Year it gets the current year with countdownDate.getFullYear()
, add one to make it next year
2. 1 - 1 = 0, so this is selecting the first month (January)
3. 26 is the 26th day of a particular month
To fix you can replace both of these lines with a single one using the string notation:
var countdownDate = new Date("November 6, 2012 20:00:00 EST");
Upvotes: 0
Reputation: 27405
countdownDate = new Date(2012, 10, 6, 20, 0, 0, 0);
or
countdownDate = new Date("11/6/2012 8:00 PM")
see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
Upvotes: 0
Reputation: 2558
The Date
constructor shown above is in the format new Date(Year, Month, Day)
. However, keep in mind that the Month
argument is 0-indexed. That means that January is 0
, not 1
. So to count down to November 6th, 2012, you would want to construct the date like this:
countdownDate = new Date(2012, 10, 6);
You might also consider doing:
countdownDate = new Date(2012, 11-1, 6);
Which is similar to what the example shows. But it just slows things down unnecessarily in order to be more clear about what month you're referring to.
Additionally, if you construct a Date
object with no arguments, it gives you the current date. So new Date()
gives you a date object equivalent to "now". In the example, they use that to get the current year using (effectively) (new Date()).getFullYear()
. They then increment it by one and pass it into a new Date
constructor in order to get the time until the "next" Australia Day.
It should be noted that the Australia Day example actually has a bug. If it's currently January, then the year will be unnecessarily incremented and the countdown will show the time until the following year's Australia Day. So in the example, the countdown will never drop below 26 days. Whoops. :-)
UPDATE:
Either your question got updated or I missed this the first time around. Looks like you wanted to end at 8PM EST. That's actually pretty tricky using the numerical date constructor. Because JavaScript runs on the client side and uses "local time" by default, you need to explicitly note the time zone. You can do this using the setUTC
versions of setters (e.g. setUTCHours()
), but it's a bit annoying and takes several lines of code. So your best bet is to use Date's String-based constructor:
new Date("November 6, 2012 20:00:00 GMT -5:00")
Upvotes: 2
Reputation: 6802
The Date() constuctor calls you'd want to use would be one of the following:
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Soy, you could do
new Date("November 6, 2012 20:00:00 GMT -5:00")
and that would do it.
Upvotes: 0