determine if a datetime value is on a different day
12 次查看(过去 30 天)
显示 更早的评论
If I have two datetime values, t1 and t2, how can I determine if t2 is on a different, and later, day than t1? The check needs to account for end-of-month and end-of-year rollovers, and the times (hr, min, sec) may not be the same between t1 and t2. I just need to know if they are are different calendar days.
Functions like between and caldiff seem to return a duration, but if that duration is less than 24 hours, this method won't work. For example, two times only 2 minutes apart but accoss a day boundary:
t1 = datetime('2025-03-01 23:59:00')
t2 = datetime('2025-03-02 00:01:00')
dt = caldiff([t2 t1],'days')
between(t1,t2,'days')
datenum would work, but MATLAB is recommending not to use. How can this be done with datetime values and functions?
0 个评论
采纳的回答
Stephen23
2025-3-3
编辑:Stephen23
2025-3-3
"Functions like between and caldiff seem to return a duration"
BETWEEN and CALDIFF will not help you. Either use DATESHIFT or the properties of the DATETIME object:
t1 = datetime('2025-03-01 23:59:00')
t2 = datetime('2025-03-02 00:01:00')
dateshift(t1,'start','day')==dateshift(t2,'start','day')
t1.Year==t2.Year && t1.Month==t2.Month && t1.Day==t2.Day
更多回答(1 个)
Walter Roberson
2025-3-3
t1 = datetime('2025-03-01 23:59:00')
t2 = datetime('2025-03-02 00:01:00')
t2 >= t1 & dateshift(t1, 'start', 'day') == dateshift(t2, 'start', 'day')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!