String to Date Subtraction
조회 수: 12 (최근 30일)
이전 댓글 표시
Hello,
How do I subtract two dates that are in a string formatted like: 'Mon Feb 27 17:03:15 EST 2017' and 'Mon Feb 27 17:40:11 EST 2017' in matlab 2010a?
댓글 수: 0
채택된 답변
Walter Roberson
2017년 2월 28일
t1 = 'Mon Feb 27 17:03:15 EST 2017';
t2 = 'Mon Feb 27 17:40:11 EST 2017'
times = datetime({t1; t2}, 'InputFormat', 'eeee MMM dd HH:mm:SS z yyyy', 'TimeZone', 'America/New_York');
time_diff = times(2) - times(1)
The result will be a duration object containing 36 minutes 59 seconds
댓글 수: 2
Walter Roberson
2017년 2월 28일
In R2010a you would need to parse the dates using datenum instead of datetime. That is going to be more difficult as datenum does not support time zones at all. Are all of the times EST, or are some of them EDT, or are there other time zones as well? Or can we count on the two times being in the same timezone?
Can we count on there always being two digits for the day of the month? Can we count on there being a leading 0 in the hour?
Peter Perkins
2017년 2월 28일
Walter, one slight change: the format should be
'eeee MMM dd HH:mm:ss z yyyy'
with a small s for seconds.
추가 답변 (1개)
Peter Perkins
2017년 2월 28일
Walter's datetime suggestion returns a duration, which represents an exact, fixed-length amount of time. Depending on what you mean by "subtract", you might also look at between to get the difference in terms of calendar units:
>> times = datetime([2016 2017],[11 2],[19 27],[14 17],[53 40],[20 0])
times =
1×2 datetime array
19-Nov-2016 14:53:20 27-Feb-2017 17:40:00
>> between(times(1),times(2))
ans =
calendarDuration
3mo 8d 2h 46m 40s
>> between(times(1),times(2),'weeks')
ans =
calendarDuration
14w
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!