0

My target is compare date plus time for start and end date time.

The following code which only handles date did not work

var startDate = "22-Apr-2016";
var endDate = "22-Apr-2016";
var stdate = new Date(startDate);
var endate = new Date(endDate);
var startTime = "10:14 AM";
var endTime = "11:14 PM";
var stTime = startTime.substring(0, 5);
var enTime = endTime.substring(0, 5);
var stAMorPM = startTime.substring(6, 8);
var enAMorPM = endTime.substring(6, 8);

if( (stdate > endate)) {
    console.log("first date is greater");
}

if((stdate == endate)) {
    console.log("Equal dates");
}

UPDATE:

Comparing dates and time for start and end datetime by concatenation get them, is supposedly not the correct way to do it.

var start = "22-Apr-2016"+ " "+startTime;
var end   = "22-Apr-2016"+ " "+endTime;
start     = new Date(start);
end       = new Date(end);
if(start.getTime() > end.getTime()){
  console.log("start greater ")
} else if(start.getTime() < end.getTime()) {
  console.log("start lesser ")
} else if(start.getTime() == end.getTime()) {
  console.log("start equals ")
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Syed
  • 2,471
  • 10
  • 49
  • 89
  • @mplungjan I'd be getting date and time as separate fields. Not in a single field. If you have a sample example, can u provide – Syed Apr 08 '16 at 05:15
  • Only a duplicate if the browser can handle new Date("DD-MMM-YYYY"), and that is just the beginning since he needs to append 10:14 AM to it – mplungjan Apr 08 '16 at 05:28
  • @syed—you can use a function like the one [*here*](http://stackoverflow.com/questions/36275843/calculating-if-date-picker-time-picker-are-in-the-past-of-eastern-standard-tim/36280407#36280407), note that it converts to UTC-0500 but you should be able to work it out. If you need help on the conversion to date, post another question. – RobG Apr 08 '16 at 05:42

5 Answers5

1

You don't have to do all that. Simply get the time in milliseconds and compare it. So it will be as such:

if (stdate.getTime() > endate.getTime()) {
     console.log("first date is greater");
}

if (stdate.getTime() == endate.getTime()) {
     console.log("Equal dates");
}
Srini.S
  • 11
  • 3
1

Two JavaScript object, in your case Date instances, can only be considered equal if they refer to the same object. Therefore, two objects, even with the same date, will always give false when comparing using ==.

If you want to compare two dates, you have to compare their timestamps. You can get them using .getTime() method:

if (stdate.getTime() == endate.getTime()) {
  console.log("Equal dates");
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • Where is his TIME in this example? – mplungjan Apr 08 '16 at 05:21
  • @mplungjan This is a correct answer. `getTime` returns the number of milliseconds since 1 January 1970. – 4castle Apr 08 '16 at 05:22
  • Where is his 10:14 AM in this example? – mplungjan Apr 08 '16 at 05:23
  • 1
    @mplungjan The question says they just want to know about the "first step". This code would work for that too though. – 4castle Apr 08 '16 at 05:25
  • Right. His immediate next problem will be to attach the time. Also how compatible with browsers is new Date("DD-MMM-YYYY)? Which is now the updated question – mplungjan Apr 08 '16 at 05:26
  • @mplungjan Why did you reopen this question? If it's only about comparing dates, the duplicate target fully answers it. If it's about adding time to a date, it should be edited to focus on the actual issue. – Michał Perłakowski Apr 08 '16 at 05:58
  • And there also already are many questions about adding hours and minutes to date, for example [this](http://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) or [this](http://stackoverflow.com/questions/13034067/add-6-hours-to-current-time-and-display-to-page). – Michał Perłakowski Apr 08 '16 at 06:01
  • @Gothdo There is no single answer to how to compare _DD dash MMM dash YYYY space HH:MM AM/PM_ - it is not trivial and not simple to find an example. OP already changed the question to include the time as important. I have updated the question. – mplungjan Apr 08 '16 at 06:09
1

Comparing dates is a good way to go about this, your problem is how you convert the strings to dates. You should not use the Date constructor (or Date.parse) to parse strings as it is largely implementation dependent and even strings consistent with ECMA-262 are not necessarily parsed correctly or give expected results.

Therefore, the first task is to parse the date strings. You can use a library, but if you only have one format to support a simple function will suffice. If the date and time are provided as separate parts, it's fairly simple to parse each part, either as a separate function or all in one, e.g.

/*  @param {string} s - date string in format dd-MMM-yyyy
**  @returns {Date} Date assuming local date
*/
function parseDate(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var b = s.match(/\w+/g) || [];
  return new Date(b[2], months[b[1].slice(0,3).toLowerCase()], b[0]);
}

/*  @param {string} s - time string in format hh:mmap
**  @returns {Number} time converted to milliseconds
*/
function parseTimeToMs(s) {
  var b = s.split(/\D/);
  var h = b[0]%12 + (/pm$/i.test(s)? 12 : 0);
  return h*3.6e6 + b[1]*6e4;
}

/*  @param {string} ds - date string in format dd-MMM-yyyy
/*  @param {string} ts - time string in format hh:mmap
**  @returns {Date} Date assuming local date built from date and time strings
*/
function getDate(ds, ts) {
  var d = parseDate(ds);
  d.setMilliseconds(d.getMilliseconds() + parseTimeToMs(ts));
  return d;
}

var startDate = '22-Apr-2016';
var endDate   = '22-Apr-2016';
var startTime = '10:14 AM';
var endTime   = '11:14 PM';

document.write('Start is: ' + getDate(startDate, startTime));
document.write('<br>End is : ' + getDate(endDate, endTime));
document.write('<br>Start before end? ' + (getDate(startDate, startTime) < getDate(endDate, endTime)));
body {
  font-family: courier, monospace;
  font-size: 80%;
}

Note that if using the relational operators < and >, the operands are coerced to number, however if doing equality comparisons with == or ===, you'll always get false as they compare the operands as objects, which are only ever equal to themselves.

So you might want to always explicitly convert the Dates to number just to be sure, the unary + operator is handy for that:

+getDate(startDate, startTime) < +getDate(endDate, endTime)

and

+getDate(startDate, startTime) == +getDate(endDate, endTime)
RobG
  • 142,382
  • 31
  • 172
  • 209
-1

Try this

var startDate = "22-Apr-2016";
var endDate = "22-Apr-2016";
var stdate = new Date(startDate);
var endate = new Date(endDate);
if( (stdate.toUTCString() > endate.toUTCString())) {
    alert("first date is greater");
}


if(stdate.toUTCString() == endate.toUTCString()) {
    alert("Equal dates");
}
M14
  • 1,780
  • 2
  • 14
  • 31
-2
if( (new Date(startDate).getDate() > new Date(endDate).getDate())){
    // to do
}

Hope this works for you.

Sameera Thilakasiri
  • 9,452
  • 10
  • 51
  • 86