compare string with todays date in javascript

邮差的信 提交于 2019-11-27 03:15:53

问题


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

Any suggestions?


回答1:


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



回答2:


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")
}



回答3:


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.




回答4:


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");



回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/15063670/compare-string-with-todays-date-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!