0

I have two dates i want to throw an alert if astart date is less than enddate format i use dd/mm/yyyy time 24 hrs format :HH:MM:SS

var strt_date  = 31/03/2014 23:02:01;
var end_date  = 01/04/2014 05:02:05;

    if(Date.parse(strt_date) < Date.parse(end_date))
    {
        alert("End datetime Cannot Be Less Than start dateime");

        return false;

    }
Dairo
  • 822
  • 1
  • 9
  • 22
mark rammmy
  • 1,478
  • 5
  • 27
  • 61

3 Answers3

1

See the following answer: Compare two dates with JavaScript

Essentially you create two date objects and you can compare them.

var start_date = new Date('31/03/2014 23:02:01');
var end_date = new Date('31/03/2014 23:02:01');
if (end_date < start_date) {
    alert("End datetime Cannot Be Less Than start dateime");

    return false;
}

(from reading the linked answer it is possible using the Date::gettime method for comparison purposes may be faster than the actual comparing of date objects)

Community
  • 1
  • 1
vogomatix
  • 4,856
  • 2
  • 23
  • 46
0

Your timestamps are not quoted as strings, which is throwing a syntax error, add single quotes to them:

var strt_date  = '31/03/2014 23:02:01';
var end_date  = '01/04/2014 05:02:05';

if((new Date(strt_date)).getTime() < (new Date(end_date)).getTime())
{
    alert("End datetime Cannot Be Less Than start dateime");

    return false;

}

Using .getTime() will compare as numbers, so you can determine if the start date has a greater number than the end date.

DEMO

MacMac
  • 34,294
  • 55
  • 151
  • 222
0

Try to use the folowing format: Date.parse("YEAR-MONTH-DAYTHOURS:MINUTES:SECONDS")

var strt_date  = "2014-03-31T23:02:01";
var end_date  = "2014-04-01T05:02:05";

if(Date.parse(strt_date) < Date.parse(end_date))
{
    alert("End datetime Cannot Be Less Than start dateime");

    return false;

}
tmarwen
  • 15,750
  • 5
  • 43
  • 62