如何在javascript中将dd / MM / yyyy格式日期与今天的日期进行比较 [英] How to Compare dd/MM/yyyy format date with today's date in javascript

查看:86
本文介绍了如何在javascript中将dd / MM / yyyy格式日期与今天的日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 dd / MM / yyyy 格式的日期(即toDate)。现在我想在今天的javascript中将它与今天的日期进行比较



  if (toDate> = todaysDate)
{
}





任何人都可以帮我解决这个问题吗?



提前感谢

(Keerthi Kumar)

解决方案

在这里看到: http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript [ ^ ]


如果你的日期是字符串,那么你必须使用拆分 [ ^ ]方法。为避免这种麻烦,您应该考虑使用像 https://jqueryui.com/datepicker/ [ ^ ]

接下来,使用适当的日期 [ ^ ]创建日期对象的方法。

尝试这个例子并适应:

< script> 
var somedayString =16/4/2015;
var dateParts = somedayString.split(/);
var day = dateParts [0];
var month = dateParts [1] - 1;
var year = dateParts [2];
//设置日期
某天=新日期(年,月,日);
alert(有一天是+某天);

//设置日期
var today = new Date();
today.setHours(0,0,0,0);
alert(今天是+今天);

if(今天<某一天){
text =今天是某天之前。;
}否则if(今天>某天){
text =今天是某天之后。;
} else {
text =有一天是今天。;
}
alert(text);
< / script>



请注意,月份数字从0开始,代表1月。


  //  在此处尝试 
var currentDate = new 日期()
var day = currentDate.getDate()
var month = currentDate.getMonth()+ 1
var year = currentDate.getFullYear()
document .write( + day + / +月+ / +年+
var todaysDate = day + / +月+ / +年


I have a date (ie toDate) in "dd/MM/yyyy" format. Now i want to compare it with today's date in javascript
like

if(toDate>=todaysDate)
{
}



can any one please help me to solve this ?

thanks in advance
(Keerthi Kumar)

解决方案

See here: http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript[^]


If you date is in string, then you have to split them out into the various date parts by using split[^]method. To avoid this hassle, you should consider using a datepicker like https://jqueryui.com/datepicker/[^]
Next, use the appropriate Date[^] methods to create the date objects.
Try this example and adapt:

<script>
var somedayString = "16/4/2015";
var dateParts= somedayString.split("/");
var day = dateParts[0];
var month = dateParts[1] - 1;
var year = dateParts[2];
// set a date
someday = new Date(year, month, day);
alert("someday is " + someday);

// set a date
var today = new Date();
today.setHours(0,0,0,0);
alert("today is " + today);

if (today < someday) {
    text = "Today is before someday.";
} else if (today > someday) {
    text = "Today is after someday.";
} else {
    text = "Someday is today.";
}
alert(text);
</script>


Note that the month numbers start from 0 which represents January.


// try here
 var currentDate = new Date()
 var day = currentDate.getDate()
 var month = currentDate.getMonth() + 1
 var year = currentDate.getFullYear()
 document.write("" + day + "/" + month + "/" + year + "")
 var todaysDate = day + "/" + month + "/" + year


这篇关于如何在javascript中将dd / MM / yyyy格式日期与今天的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆