5

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.

5
  • am just updating the value of $("#EndDate").val() is like that Commented May 13, 2014 at 11:15
  • Updating a value would be $("#EndDate").val("5/13/2014") Commented May 13, 2014 at 11:15
  • no putvande ..am just saying the value of element is like but it is not a code understood. Commented May 13, 2014 at 11:17
  • 1
    @dowvoter without reason putting down votes is not good. Commented May 13, 2014 at 11:57
  • Possible duplicate of How to check if input date is equal to today's date? Commented Sep 18, 2018 at 5:28

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Try as below:

var end_date    = "05/12/2014"
if(new Date() > new Date(end_date))
{   
 alert('End date should be greater than Start date');
}

fiddle

Comments

1

i found solution my self by modifying code like this

if (Date.parse(new Date().setHours(0, 0, 0, 0)) > Date.parse($("#EndDate").val())) {
//
}

to avoid time comparison. is there any other better way to do compare dates in efficient manner without any plugin.

1 Comment

@RobG could you please update what is the best way to compare dates ?
0
var startDate = moment.parse("18/02/2013", "DD/MM/YYYY");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = dd+'/'+mm+'/'+yyyy;


if (today.isAfter(startDate)) {
...
}

Comments

0

Your asking for

check my date is greater than current date

and your giving us this ..

if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) { 
    //condition satisfied for today date too.
}

just have to reverse your condition like that :

if (Date.parse(new Date()) < Date.parse($("#EndDate").val())) { 
    //condition satisfied for today date too.
}

Comments

0

However, I am late to answer this, but here is my approach to solve this issue:

var TodayDate = new Date();
var endDate= new Date(Date.parse($("#EndDate").val()));

if (endDate> TodayDate) {
    // throw error here..
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.