Midnight becomes noon with datetime function

13 ビュー (過去 30 日間)
Evan Dailey
Evan Dailey 2018 年 8 月 6 日
コメント済み: Peter Perkins 2018 年 8 月 24 日
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 件のコメント
Evan Dailey
Evan Dailey 2018 年 8 月 6 日
Wow, that's strange we get different results with the same file on the same version.
I'm using Windows with:
>> version
ans =
'9.4.0.813654 (R2018a)'
Walter Roberson
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.

サインインしてコメントする。

採用された回答

Evan Dailey
Evan Dailey 2018 年 8 月 6 日
編集済み: Evan Dailey 2018 年 8 月 6 日
I think it figured it out: in Preferences, under Command Window, there's a Datetime format. I changed it from "uuuu MM dd hh:mm:ss" to "uuuu-MM-dd HH:mm:ss" -- the key being "HH" rather than "hh". And now:
>> a
a =
datetime
2016-03-01 00:00:00
Easy answer, if that's really all that's going on. I just assumed datetime values were the values, not formatted strings.
  2 件のコメント
Stephen23
Stephen23 2018 年 8 月 7 日
"I just assumed datetime values were the values, not formatted strings."
What are "the values" ?
Any time you look at a number or character vector or anything else it is always a representation of the data stored in memory. Unless you enjoy looking at binary bits, all displayed data you look at is displayed according to some set of rules and assumptions: you cannot "see" real datetime "values" any more than you can "see" a real floating point number. Important to note is that representations of the underlying data are equally valid, in just the same way that
3.1
3.14159
0.314159E1
pi
are all equally valid ways to represent the same number. None of them is more "correct" than the other, they just follow different rules and assumptions.
What is the point of this? That any displayed representation of a datetime object will always be formatted according to some rules, so no matter whether you see it as a formatted date string, or a number, or days since some epoch, all of these represent the same data and all follow rules and assumptions. None of them are "the values", they are all just visual representation of the datetime object.
Peter Perkins
Peter Perkins 2018 年 8 月 24 日
If your preference is a 12hr clock (it seems like that was unintentional, but if), you can use hh in your display preference, but you'll want to ad an 'a' at the end of the format to show am/pm.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by