compare string with today's date in JavaScript

Leon van der Veen picture Leon van der Veen · Feb 25, 2013 · Viewed 95.1k times · Source

I've got a string from an input field which I use for date with a format like this 25-02-2013. Now I want to compare the string with today's date. I want to know if the string is older or newer then today's date.

Any suggestions?

Answer

polin picture polin · Feb 25, 2013
    <script type="text/javascript">

var q = new Date();
var m = q.getMonth()+1;
var d = q.getDay();
var y = q.getFullYear();

var date = new Date(y,m,d);

mydate=new Date('2011-04-11');
console.log(date);
console.log(mydate)

if(date>mydate)
{
    alert("greater");
}
else
{
    alert("smaller")
}


</script>