Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to check my date is greater than current date

$("#EndDate").val() ="5/13/2014" ->M/d/y

Please find below code

if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) { 

 //condition satisfied for today date too.

}

so my end date is today date.but still current date greater than end date. why ? how can i check and validate this. i understood some time value is greater than end date. but i want to check only date/month/year not time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
136 views
Welcome To Ask or Share your Answers For Others

1 Answer

If:

$("#EndDate").val();

returns a string in m/d/y format, you can turn that into a date object using:

function parseDate(s) {
  var b = s.split(/D/);
  return new Date(b[2], --b[0], b[1]);
}

To create a comparable date, do what you are already doing:

var today = new Date();
today.setHours(0,0,0,0);

So now you can do:

if (parseDate($("#EndDate").val()) > today) {
  // date is greater than today
}

or if you really must:

if (+parseDate($("#EndDate").val()) > new Date().setHours(0,0,0,0)) ...

Please note that when you do:

Date.parse(new Date().setHours(0, 0, 0, 0))

firstly a new date is created from new Date(). Calling setHours() sets the time value, but the return value from the call is the UTC time value of the Date object.

Date.parse expects a string that looks something like a date and time, so if you pass it a number time value something like 1399903200000, the implementation will fall back to some implementation heuristics to either turn it into a Date or NaN.

So please don't do that. Parsing any string with Date.parse is implementation dependent (even the strings specified in ECMA5) and will return different results in different browsers. So please don't do that either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...