javascript 在javascript中将字符串与今天的日期进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15063670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compare string with todays date in javascript
提问by Leon van der Veen
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.
我从输入字段中获取了一个字符串,我将其用于日期,格式为 25-02-2013。现在我想将字符串与今天的日期进行比较。我想知道字符串比今天的日期旧还是新。
Any suggestions?
有什么建议?
采纳答案by polin
<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>
回答by Arepalli Praveenkumar
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")
}
回答by Marquizzo
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.
我添加0:00:00
到第二个变量的原因是因为没有它,它将与 UTC(格林威治)时间进行比较,这可能会给您带来不希望的结果。如果您将时间设置为 0,那么它将与用户当地的午夜进行比较。
回答by 999k
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.
使用 JavascriptDate
对象对您来说会更容易。但由于Date
对象不支持您的格式,我认为你必须分析你输入的字符串(例如:25-02-2013)以“ - ”来获得date
month
和year
再利用Date
的对象进行比较。
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");
回答by TheTurkey
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:
如果您的日期输入格式为“25-02-2013”,您可以使用 split() 方法将字符串拆分为 DD、MM 和 YYYY:
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.
parseInt() 函数用于将字符串转换为整数。然后可以将这 3 个变量与 Date() 对象的属性进行比较。
回答by mitpatoliya
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.
好吧,这可以进一步优化,但这应该让您清楚地了解以统一格式制作日期所需的内容。