Midnight becomes noon with datetime function
Show older comments
I've run into a problem when converting date and time values to Matlab datetime values. The datetime function seems to force midnight (00:00) to noon (12:00). But, when comparing the outputs that look identical, Matlab says they are not. For example:
>> a = datetime(2016,03,01,0,0,0)
a =
datetime
2016 03 01 12 00 00
>> b = datetime(2016,03,01,12,0,0)
b =
datetime
2016 03 01 12 00 00
>> a == b
ans =
logical
0
How is this possible? Am I just missing something, or is there a good solution to correct or avoid this? This seems scary becaues if I'm using the output values in a larger script, how do I know that Matlab isn't confusing midnight and noon, and how would I be able to actually tell which is which?
6 Comments
James Tursa
on 6 Aug 2018
Edited: James Tursa
on 6 Aug 2018
What does this show
which datetime
What version of MATLAB are you using?
Evan Dailey
on 6 Aug 2018
Peter Meglis
on 6 Aug 2018
Evan,
What version of Matlab are you running? I just tried this on 2018a and got:
>> a = datetime(2016, 03, 01, 0, 0, 0)
a =
datetime
01-Mar-2016 00:00:00
James Tursa
on 6 Aug 2018
Edited: James Tursa
on 6 Aug 2018
I can't reproduce your error on my PC. In fact, the format of the displayed results don't even match. My results:
>> a = datetime(2016,03,01,0,0,0)
a =
datetime
01-Mar-2016 00:00:00
>> which datetime
C:\Program Files\MATLAB\R2018a\toolbox\matlab\timefun\@datetime\datetime.m % datetime constructor
>> version
ans =
'9.4.0.813654 (R2018a)'
Mac or PC?
Evan Dailey
on 6 Aug 2018
Walter Roberson
on 7 Aug 2018
What is your system region set to? That could determine the default format for date representation. There is also a Preference about region that can affect the default format.
Accepted Answer
More Answers (0)
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!