25

I have the following :

var fit_start_time  = $("#fit_start_time").val(); //2013-09-5
var fit_end_time    = $("#fit_end_time").val(); //2013-09-10

if(Date.parse(fit_start_time)>=Date.parse(fit_end_time)){
    alert("Please select a different End Date.");
}

The above code doesn't work. Is there any other solution to the above one?

formatting date as per Where can I find documentation on formatting a date in JavaScript? also dint work for me.

Community
  • 1
  • 1
Roy M J
  • 6,926
  • 7
  • 51
  • 78

8 Answers8

68

It's quite simple:

if(new Date(fit_start_time) <= new Date(fit_end_time))
{//compare end <=, not >=
    //your code here
}

Comparing 2 Date instances will work just fine. It'll just call valueOf implicitly, coercing the Date instances to integers, which can be compared using all comparison operators. Well, to be 100% accurate: the Date instances will be coerced to the Number type, since JS doesn't know of integers or floats, they're all signed 64bit IEEE 754 double precision floating point numbers.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Actually the same date validation fails . You need to actually do it separately to achieve this. http://jsfiddle.net/neBHQ/3/ – Roy M J Sep 20 '13 at 10:43
  • 1
    @RoyMJ: your fiddle is flawed: you'll never alert 1, because the dates can't be _both_ `<` _and_ `==`. That, and `==` won't coerce the objects to integers, so you're comparing two distinct objects, and therefore will always be false (try `new Date('2013-09-1') == new Date('2013-09-1')` will always be false). Your fiddle should've been [this](http://jsfiddle.net/jMeMD/) – Elias Van Ootegem Sep 20 '13 at 10:54
  • 1
    @RoyMJ I was just about to write what Elias Van Ootegem has written. You haven't duplicated his code in jsFiddle, you're added an && clause to your condition, making it *have* to be equal as well as be less. This is moronic on so many levels. – shennan Sep 20 '13 at 10:58
  • @EliasVanOotegem its not working when using different months. If I am using fit_start_time date of November and fit_end_time is date of December. Then it is not validating. Please Help. – techV Dec 04 '15 at 08:36
  • Working only in same month date. – Sandeep Shekhawat Jun 16 '16 at 05:25
6

To comapre dates of string format (mm-dd-yyyy).

    var job_start_date = "10-1-2014"; // Oct 1, 2014
    var job_end_date = "11-1-2014"; // Nov 1, 2014
    job_start_date = job_start_date.split('-');
    job_end_date = job_end_date.split('-');

    var new_start_date = new Date(job_start_date[2],job_start_date[0],job_start_date[1]);
    var new_end_date = new Date(job_end_date[2],job_end_date[0],job_end_date[1]);

    if(new_end_date <= new_start_date) {
      // your code
    }
Amal Kumar S
  • 15,555
  • 19
  • 56
  • 88
  • 1
    doesn't javascript takes month from 0 index? And shouldn't you subtract `-1` from the month part of your `Date` object ? correct me if I am wrong. – sgauri Jun 27 '18 at 21:35
  • month index starts from 0 so ,console.log(new Date('2014','09','1')) will show Oct 1, 2014. Above solution worked for me but i have to pass (month-1) for month argument. If Month value is greater than 11 then we will unexpected date. – karunakar bhogyari Dec 10 '19 at 09:22
3

As in your example, the fit_start_time is not later than the fit_end_time.

Try it the other way round:

var fit_start_time  = $("#fit_start_time").val(); //2013-09-5
var fit_end_time    = $("#fit_end_time").val(); //2013-09-10

if(Date.parse(fit_start_time) <= Date.parse(fit_end_time)){
    alert("Please select a different End Date.");
}

Update

Your code implies that you want to see the alert with the current variables you have. If this is the case then the above code is correct. If you're intention (as per the implication of the alert message) is to make sure their fit_start_time variable is a date that is before the fit_end_time, then your original code is fine, but the data you're getting from the jQuery .val() methods is not parsing correctly. It would help if you gave us the actual HTML which the selector is sniffing at.

shennan
  • 10,798
  • 5
  • 44
  • 79
  • @RoyMJ It depends what you mean by 'work'. It works, it's just not giving you the behaviour you expect. – shennan Sep 20 '13 at 10:44
2

try with new Date(obj).getTime()

if( new Date(fit_start_time).getTime() > new Date(fit_end_time).getTime() )
{
    alert(fit_start_time + " is greater."); // your code
}
else if( new Date(fit_start_time).getTime() < new Date(fit_end_time).getTime() )
{
    alert(fit_end_time + " is greater."); // your code
}
else
{
    alert("both are same!"); // your code
}
NaYaN
  • 1,300
  • 7
  • 11
  • Actually this works if you separate the `>` and `=` comparisons into two steps.. wierd. – Roy M J Sep 20 '13 at 10:42
  • This code creates the same `Date` instances over and over again, please, don't... just assing them to `fit_start_time` and `fit_end_time`, to avoid pointless overhead – Elias Van Ootegem Sep 20 '13 at 11:26
0

This is already in :

Age from Date of Birth using JQuery

or alternatively u can use Date.parse() as in

var date1 = new Date("10/25/2011");
var date2 = new Date("09/03/2010");
var date3 = new Date(Date.parse(date1) - Date.parse(date2));
Community
  • 1
  • 1
0

Try it the other way:

var start_date  = $("#fit_start_time").val(); //05-09-2013
var end_date    = $("#fit_end_time").val(); //10-09-2013
var format='dd-MM-y';
    var result= compareDates(start_date,format,end_date,format);
    if(result==1)/// end date is less than start date
    {
            alert('End date should be greater than Start date');
    }



OR:
if(new Date(start_date) >= new Date(end_date))
{
    alert('End date should be greater than Start date');
}
0
let Date1 = "04-18-2023" // 18 April, 2023 
let Date2 = "05-01-2023" // 01 May, 2023 
if(new Date(Date1) < new Date(Date2)) 
{ 
 console.log("Date1 is less than Date2")
}
Rehan Rajpoot
  • 158
  • 2
  • 6
-1

var startDate= "11-11-2014"; // 10th Nov 2021

var endDate= "11-1-2014";

startDate= startDate.split('-');

endDate= endDate.split('-');

var newStartDate= new Date(startDate[2],startDate[0]-1,startDate[1]);

var newEndDate= new Date(endDate[2],endDate[0]-1,endDate[1]);

if(newEndDate<= newStartDate) {

}

  • This looks more like a response to [this answer and its comments](https://stackoverflow.com/a/26655449/2227743) rather than a new answer... – Eric Aya Aug 31 '21 at 12:11