Compare two date formats in javascript/jquery
Asked Answered
I

8

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.

Inspan answered 20/9, 2013 at 9:43 Comment(0)
S
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.

Squad answered 20/9, 2013 at 9:49 Comment(5)
Actually the same date validation fails . You need to actually do it separately to achieve this. jsfiddle.net/neBHQ/3Inspan
@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 thisSquad
@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.Differential
@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.Rangefinder
Working only in same month date.Cirrose
A
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
    }
Adversative answered 30/10, 2014 at 14:17 Comment(2)
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.Tisbe
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.Mistress
D
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.

Differential answered 20/9, 2013 at 9:47 Comment(1)
@RoyMJ It depends what you mean by 'work'. It works, it's just not giving you the behaviour you expect.Differential
M
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
}
Mantilla answered 20/9, 2013 at 9:46 Comment(2)
Actually this works if you separate the > and = comparisons into two steps.. wierd.Inspan
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 overheadSquad
A
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));
Arborvitae answered 20/9, 2013 at 9:51 Comment(0)
M
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');
}
Marriott answered 20/9, 2013 at 10:10 Comment(1)
i think u missed out compareDates function if your above code has to work.Inspan
N
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")
}
Nertie answered 17/4, 2023 at 20:9 Comment(0)
L
-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) {

}

Lyris answered 31/8, 2021 at 7:49 Comment(1)
This looks more like a response to this answer and its comments rather than a new answer...Devault

© 2022 - 2024 — McMap. All rights reserved.