0

I know that date format "dd/mm/yyyy" can be achieved like this:

var d = new Date();
var day = d.getDate();
var month = (d.getMonth() +1);
var year = d.getFullYear();

document.write("Today is " +day+ "/" +month+ "/" +year+ "<br>");

Date picker

var x = new Date(document.getElementById("dateSelection"));

However how can I convert those to a single date object so I can then compare it against date picker in a simple statement like this:

if (d > x)
{
   document.write("Date from the past");
}
else if (d < x)
{
   document.write("Date from the future");
}
else
{
   document.write("Date equals today's date");
}

Thanks for help I'm novice at this.

j08691
  • 204,283
  • 31
  • 260
  • 272
Raikonne
  • 195
  • 1
  • 1
  • 7

2 Answers2

1

You are passing the dateSelection element to new Date, not its value. Date wants a string as its parameter, not a DOMElement.

Try this:

var x = new Date(document.getElementById("dateSelection").value);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Use a handy dandy prototype to convert the string to a date:

String.prototype.toDate=function(){
   var mo = parseInt(this.substr(0,2)) -1
   var dy = this.substr(3,2)
   var yr = this.substr(6,4)
   var dt  = new Date(yr, mo, dy, 0, 0, 0, 0)
   return dt
}

var s = '01/01/2014'
var d = s.toDate()

Now you have a date and can do date comparisons.

Brian McGinity
  • 5,777
  • 5
  • 36
  • 46