[英]Compare if 2 dates are the same in javascript
var beginningTime = moment('1635750314812', 'YYYY/MM/DD'); // Today date var endTime = moment(undefined, 'YYYY/MM/DD') // Today date console.log(beginningTime.isSame(endTime)); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I am using moment js to get if 2 date are the same using the above pattern.我正在使用 moment js 来获取使用上述模式的 2 个日期是否相同。 But now i get false instead of true.
但现在我得到假而不是真的。 How to solve the issue?
如何解决问题?
Instead of formatting the beginningTime and endTime separately, you should take a look at the 'granularity' argument for isSame() .您应该查看isSame()的“粒度”参数,而不是分别格式化开始时间和结束时间。
I made a few adjustments to your code for it to work, and also look cleaner.我对您的代码进行了一些调整以使其正常工作,并且看起来更干净。
var beginningTime = moment(1635750314812); // Today date (from timestamp) var endTime = moment() // Today date console.log(beginningTime.isSame(endTime, 'day')); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
It first simply creates a moment
of your timestamp and a moment
of right now, whenever now is.它首先简单地创建一个
moment
你的时间戳和的moment
的,现在,只要是现在。 Then it will compare the two moment
s with a granularity of a day, effectively checking if the day and everything bigger (month, year) is the same.然后它将以一天的粒度比较两个
moment
,有效地检查一天和所有更大的(月,年)是否相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.