Javascript if date within range
October 29, 2008 1:50 PM   Subscribe

Javascript: If date is between date range, output this, else output this.

I need some javascript that outputs certain code if the date is between a certain date range. If it is not, it would output something else.

Today is 10-29-08

Date range in script would be something like 10-01-08 to 10-31-08

If we are in that range, it would output 'this code.'

If we are not in that date range, it would output 'different code.'
posted by B(oYo)BIES to Computers & Internet (6 answers total) 2 users marked this as a favorite
 
// Parse the dates. A little tricky because of your format

leftBound = "10-14-08";
splitLeftBound = leftBound.split("-");

rightBound = "10-16-08";
splitRightBound = rightBound.split("-");

testDate = "10-15-08";
splitDate = testDate.split("-");

// Convert to number of seconds since 1970 - Note this breaks after 2099
leftTest = new Date(2000 + parseFloat(splitLeftBound[2]), splitLeftBound[0] - 1, splitLeftBound[1]).getTime();
rightTest = new Date(2000 + parseFloat(splitRightBound[2]), splitRightBound[0] - 1, splitRightBound[1]).getTime();
test = new Date(2000 + parseFloat(splitDate[2]), splitDate[0] - 1, splitDate[1]).getTime();

//You now have 3 comparable integers

if (test <> alert("Outside of left bound");
} else if (test > rightTest) {
alert("Outside of right bound");
} else {
alert("Number is good!");
}
posted by bitdamaged at 2:27 PM on October 29, 2008


if (myDate > today) document.write('today is before myDate');
else document.write('today is on or after myDate'); 
But do you really want to use Javascript for that? That not only requires a Javascript client, it relies on the client's idea of what day it is.

Conditional SSI is a lot more robust and under your control.
posted by rokusan at 2:28 PM on October 29, 2008


You're probably going to want to use a Date object. I've created a demo at my website but I'm not sure what the policy for self-linking is in comments on AskMe, so I'll post the relevant function here and PM you a link to the full HTML document if you'd like to take a closer look.

The code:
function dateStringToTime(dateStr) {
	year = dateStr.substring(6, 8);
	month = dateStr.substring(0, 2);
	day = dateStr.substring(3, 5);
	
	return new Date(year, month, day).getTime();
}
This will turn your date string into a Unix timestamp. Then it can be numerically compared numerically with basic operators. i.e.:
if(date1 > date2) { ... }

posted by MaxK at 2:54 PM on October 29, 2008


Second using a Date object: there's no point sticking the date in a string if there's a purpose-built container for it. And the Javascript Date object even has a comparison between two dates!

See here
posted by katrielalex at 3:14 PM on October 29, 2008


This function is broken
function dateStringToTime(dateStr) {

	year = dateStr.substring(6, 8); // 08 will not parse correctly as a year

	month = dateStr.substring(0, 2); // Date function uses months from 0-11

	day = dateStr.substring(3, 5);


	return new Date(year, month, day).getTime();

}

posted by bitdamaged at 3:58 PM on October 29, 2008


Confirming bitdamaged. You want:
year  = parseInt(dateStr.substring(6, 8), 10);
month = parseInt(dateStr.substring(0, 2), 10) - 1;
day   = parseInt(dateStr.substring(3, 5), 10);
The parseInt() call turns the string "02" into the integer 2, and the second argument to parseInt() means you want to force JavaScript to use base 10 (it will try to use octal conversion on "08" and "09" otherwise, because of the leading zero, and complain about invalidity).

Although, if you're not certain that your month and day pieces are going to be two digits (e.g. if you might get "1-2-2008" for January 2, 2008), you would be better off using split() to break up the string into chunks upon which to run parseInt().
var datePieces = dateStr.split("-");
var month = parseInt(datePieces[0], 10) - 1,
    day   = parseInt(datePieces[1], 10),
    year  = parseInt(datePieces[2], 10);
This just covers the string-to-date conversion part of the problem. You've gotten the right advice already as to the rest of it: create real Date objects and use those objects' built-in comparison function.
posted by letourneau at 6:55 PM on October 29, 2008


« Older What video editing software is best for me?   |   Insanely loud MRI -- why? Newer »
This thread is closed to new comments.