0

I want to compare following to dates i.e. d1 with d2:

var d1 = new Date(12,05,2013);
var d2 = "12/05/2013";
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • 10
    d1 is a date. d2 is a string. – fred02138 Sep 09 '13 at 12:10
  • 1
    Convert a string to a date: http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js – Barrie Reader Sep 09 '13 at 12:11
  • 6
    `d1` is also incorrect the [spec.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) says `Date(year, month, day [, hrs, mins, secs, ms]);` – Emissary Sep 09 '13 at 12:12
  • possible duplicate of [How do I compare dates in Javascript?](http://stackoverflow.com/questions/16873689/issue-with-date-variable-in-javascript/16873741) or [Compare dates with JavaScript](http://stackoverflow.com/questions/492994/compare-dates-with-javascript) – Bergi Sep 09 '13 at 12:12

2 Answers2

4

convert date to timestamp

DateObject.getTime(); will give timestamp

and convert string to date new Date(d2)

javScript

var d1 = new Date("12/05/2013");
var d2 = "12/05/2013";
console.log(d1.getTime());
console.log(new Date(d2).getTime());

if(d1.getTime() == new Date(d2).getTime()){
   //do something
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

To compare two variables you need to use JS operators, consider:

if(d1==d2)
{
 //do this
}

else
{
 //do this
}

More information on logical operators can be found here: http://www.w3schools.com/js/js_comparisons.asp

Myles
  • 926
  • 2
  • 12
  • 29