-1

I have a entry form in which I can allow today's date and future date but can't allow yesterday and previous days. I need to do this validation using java-script on client side.

var futuredate = new Date($("#Deadline").val());
var today = new Date();

This gives me date with time. Mon Nov 04 2013 00:00:00 GMT+0530 (India Standard Time) I want only date to compare as when user enters today's date entered date is : Fri Apr 05 2013 00:00:00 GMT+0530 (India Standard Time) current date from new Date():Fri Apr 05 2013 16:53:00 GMT+0530 (India Standard Time)

I tried using sethours() it worked fine for current month, but as it converts date in number it creates problem when date entered is of previous or any before month. for example: the date entered date is 23/3/2013 and today is 5/4/2013 it considers the entered date as valid.

var futuredate = new Date($("#Deadline").val()).setHours(0, 0, 0, 0);
var today = new Date().setHours(0, 0, 0, 0);
if (futuredate >= today) {
   alert("Valid");
}
else if (futuredate < today) {
   alert("InValid");
}
5
  • There are loads of answers on comparing dates have a look here - stackoverflow.com/questions/11170054/… Commented Apr 5, 2013 at 11:10
  • i want to compare date without time... I already surfed on stack for answers... they didn't solved my problems... Commented Apr 5, 2013 at 11:12
  • @bluesky what will be your input date, just provide an example. Commented Apr 5, 2013 at 11:13
  • @bluesky i've added an answer, please give it a go Commented Apr 5, 2013 at 11:18
  • This has nothing to do with the jquery-validate plugin. Please be more careful when tagging your questions. Edited. Thanks. Commented Apr 5, 2013 at 14:11

2 Answers 2

2

Try the following, it uses a little function to parse your datestring into a date object before using setHours(). This will work if your string is in format dd/mm/yyyy or dd-mm-yyyy.

var futuredate = parseDate(input).setHours(0, 0, 0, 0);
var today = new Date().setHours(0, 0, 0, 0);
if (futuredate >= today) {
   alert("Valid");
}
else if (futuredate < today) {
   alert("InValid");
}

//function to return a date object from the date string passed to it
function parseDate(input) {
    var parts = input.match(/(\d+)/g);
    return new Date(parts[2], (parts[1]-1), parts[0]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

just split the date and set the date function concept .using this way we can easily compare the the date

 var a =futuredate.split('/');
 var b =today.split('/');
 var date1 = new Date(a[2],a[1],a[0]);
 var date2 = new Date(b[2],b[1],b[0]);

if (date1 >= date2) {
   alert("Valid");
}
else if (date1 < date2) {
   alert("InValid");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.