Pregunta

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?

¿Fue útil?

Solución

    <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>

Otros consejos

Exact date comparsion and resolved bug from accepted answer

var q = new Date();
var m = q.getMonth();
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")
}

You can use a simple comparison operator to see if a date is greater than another:

var today = new Date();
var jun3 = new Date("2016-06-03 0:00:00");

if(today > jun3){
    // True if today is on or after June 3rd 2016
}else{
    // Today is before June 3rd
}

The reason why I added 0:00:00 to the second variable is because without it, it'll compare to UTC (Greenwich) time, which may give you undesired results. If you set the time to 0, then it'll compare to the user's local midnight.

Using Javascript Date object will be easier for you. But as the Date object does not supports your format i think you have to parse your input string(eg: 25-02-2013) with '-' to get date month and year and then use Date object for comparison.

var x ='23-5-2010';
var a = x.split('-');
 var date = new Date (a[2], a[1] - 1,a[0]);//using a[1]-1 since Date object has month from 0-11
 var Today = new Date();
 if (date > Today)
    alert("great");
 else
    alert("less");

If your date input is in the format "25-02-2013", you can split the string into DD, MM and YYYY using the split() method:

var date_string="25-02-2013";

var day  = parseInt(date_string.split("-")[0]);
var month= parseInt(date_string.split("-")[1]);
var year = parseInt(date_string.split("-")[2]);

The parseInt() function is used to make the string into an integer. The 3 variables can then be compared against properties of the Date() object.

The most significant points which needs to be remembered while doing date comparison

  • Both the dates should be in same format to get accurate result.
  • If you are using date time format and only wants to do date comparison then make sure you convert it in related format.

Here is the code which I used.

           var dateNotifStr = oRecord.getData("dateNotif");
           var today = new Date();       
           var todayDateFormatted = new Date(today.getFullYear(),today.getMonth(),today.getDate());

           var dateNotif=new Date(dateNotifStr);
           var dateNotifFormatted = new Date(dateNotif.getFullYear(),dateNotif.getMonth(),dateNotif.getDate());

Well, this can be optimized further but this should give you clear idea on what is required to make dates in uniform format.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top