Midnight becomes noon with datetime function
显示 更早的评论
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 个评论
James Tursa
2018-8-6
编辑:James Tursa
2018-8-6
What does this show
which datetime
What version of MATLAB are you using?
Evan Dailey
2018-8-6
Peter Meglis
2018-8-6
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
2018-8-6
编辑:James Tursa
2018-8-6
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
2018-8-6
Walter Roberson
2018-8-7
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!