-2

I have 2 date:

start date

<input type="text" name="startdate" value="2016-04-08">

End date

<input type="text" name="enddate" value="2016-09-02">

but how to use javascript to validate days of the end date to not less than the days from start date.

like

startdate '08' = end date '02' and 'not ok'
or
startdate '08' = end date '08' and 'ok'

thankyou

elreeda
  • 4,525
  • 2
  • 18
  • 45
febri qa
  • 43
  • 1
  • 6

2 Answers2

0

If you only have to compare the days, all you have to do is checking if one value is equal to, or higher than the other.

You can accomplish this by getting both values, take the last part, and compare those. For example:

var date = '2016-04-08';
var day = date.split('-')[2];

split turns the string into an array: MDN

The last value is the day. If you want to compare it as a number, make sure to turn it into a number (or int): MDN

DEMO

If you want to compare the whole date, which makes more sense IMO, it's a bit more tricky. In that case you have to turn the values into proper JS Date objects. Once they are both Date objects, you can compare them.

For this you can also use the split part as above. Provide the three parts (year, month, day) to the Date object to create a valid date object.

DEMO

Pimmol
  • 1,871
  • 1
  • 8
  • 14
0

you can do like these,

function CheckSchedulingDates() {
  var SD = document.getElementById('startdate').value;
  var ED = document.getElementById('enddate').value;
  if (Date.parse(SD) >= Date.parse(ED)) {
    return 'The ending date must occur after the starting date.'    
  }
}
Bharat
  • 5,869
  • 4
  • 38
  • 58
  • No, don't use *Date.parse*, see [*How to get correct Data object from string in JavaScript*](http://stackoverflow.com/questions/36488639/how-to-get-correct-data-object-from-string-in-javascript/36489088#36489088). – RobG Apr 08 '16 at 14:21