如何检查两个日期是否不在同一个日历日 [英] How to check if two dates not on the same calendar day

查看:139
本文介绍了如何检查两个日期是否不在同一个日历日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查两个日期是否不在同一天。我想出了这个解决方案,但也许有更好的方式来做到这一点:

  var actualDate = new Date(); 
var isNotToday = dateToCheck.getDay()!== actualDate.getDay()|| dateToCheck<实际日期 - 24 * 60 * 60 * 1000;


解决方案

另一个选项是使用。 toDateString()函数来将这两个日期解析为字符串。功能格式输出为:1993年7月28日星期三然后,你可以比较这两个日期字符串。

  actualDate.toDateString()=== dateToCheck.toDateString()
/ /返回true如果actualDate是同一天dateToCheck

这里的一个Plunker:



'p> http://plnkr.co/edit/J5Dyn78TdDUzX82T0ypA



'p>多由MDN:



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString


How to check if two dates not on the same day. I came up with this solution but maybe there is a better way to do this:

 var actualDate = new Date();
 var isNotToday = dateToCheck.getDay() !== actualDate.getDay() || dateToCheck < actualDate - 24 * 60 * 60 * 1000;

解决方案

Another option is using .toDateString() function to parse both dates into strings. The function formats output to: "Wed Jul 28 1993." Then you can compare both date strings.

actualDate.toDateString() === dateToCheck.toDateString()
// returns true if actualDate is same day as dateToCheck

Here's a Plunker:

http://plnkr.co/edit/J5Dyn78TdDUzX82T0ypA

And more from MDN:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

这篇关于如何检查两个日期是否不在同一个日历日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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