Why does datetime return repeated values?
1 view (last 30 days)
Show older comments
MathWorks Support Team
on 5 Sep 2019
Answered: MathWorks Support Team
on 27 Jan 2020
When I execute the following code snippet in MATLAB
start_time = datetime('05-Nov-2017 00:00:00','TimeZone','America/Los_Angeles');
end_time = datetime('05-Nov-2017 03:00:00','TimeZone','America/Los_Angeles');
DateString = start_time:1/2/24:end_time;
This ought to result in 7 times: 00:00, 00:30, 1:00, 1:30, 2:00, 2:30, and 3:00 on the same day.
Instead, it results in 9 times: 00:00, 00:30, 1:00, 1:30, 1:00, 1:30, 2:00, 2:30, and 3:00 on the same day.
If I do the same thing for the previous day, Nov 4, 2017, everything is fine...
start_time = datetime('04-Nov-2017 00:00:00','TimeZone','America/Los_Angeles');
end_time = datetime('04-Nov-2017 03:00:00','TimeZone','America/Los_Angeles');
DateString = start_time:1/24:end_time;
Gives 1 x 4 datetime array: 04-Nov-2017 00:00:00 04-Nov-2017 01:00:00 04-Nov-2017 02:00:00 04-Nov-2017 03:00:00
How can I explain this behavior?
Accepted Answer
MathWorks Support Team
on 5 Sep 2019
This is due to Daylight Saving Time (DST).
In 2017, for the United States, Daylight Saving Time (DST) ended on Sunday, November 5. Clocks were set back 1 hour to standard time from 02:00 (2 am) to 01:00 (1 am), local time on this day.
This leads to the repeated values, which is the expected behavior.
0 Comments
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!