Reputation: 2685
How do I verify in jQuery that a date entered on a input text box or value in html such as mm/dd/yyyy is not greater than todays date. Example would be "5/5/2011" would pass as true b/c its not greater than today "6/17/2011", but "5/5/3011" would be greater than today and should return false. Is there a simple function that can return true or false if a date in form "mm/dd/yyyy" is greater than todays date in javascript or jQuery?
Upvotes: 2
Views: 5077
Reputation: 8409
Don't even need jQuery, just Date objects.
Date.parse("6/17/2011") > Date.parse("5/5/3011")
Upvotes: 2
Reputation: 7663
First off, you need to be sure that dates have a universal format. 10/1/2011 is in the future in the US (October 1st, 2011) but in the past in the UK (10th January 2011). If that's satisfied, the below will work just fine:
function greaterThanToday(datestring) {
var today = new Date();
var date = new Date(datestring);
return (date > today);
}
alert("5/5/2011: " + greaterThanToday("5/5/2011"));
alert("5/5/3011: " + greaterThanToday("5/5/3011"));
Upvotes: 2
Reputation: 8407
var d = new Date("5/5/2011")
var d2 = new Date()
var b = d < d2 // << true...
Upvotes: 2
Reputation: 134157
Just convert to a Date
object and compare your date value to Date.now
- for example:
alert( Date.parse("3/1/2012") > Date.now())
You do not actually need to use jQuery for this at all, pure JavaScript is good enough. Please have a look at the docs for Javascript Date
for more information.
Upvotes: 8