setting default date and time format in R2015a ?

1 回表示 (過去 30 日間)
Liqun
Liqun 2015 年 5 月 18 日
コメント済み: Peter Perkins 2015 年 5 月 26 日
I have problems to set default date format in matlab R2015a version. After I enter the command to change the default date format, it seems not working:
datetime.setDefaultFormats('defaultdate','yyyy-MM-dd'); date
ans =
18-May-2015
datetime.setDefaultFormats('defaultdate','MM/dd/yyyy'); date
ans =
18-May-2015
The date format have not changed! What I did wrong here?

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 5 月 18 日
The date() call does not return a datetime object: it returns a string in a fixed format. datetime() called without any parameters returns the current date and time as a datetime object.
  2 件のコメント
Liqun
Liqun 2015 年 5 月 19 日
But, when I change date to datetime(), the date format have no change also. see below: datetime.setDefaultFormats('defaultdate','yyyy-MM-dd'); datetime()
ans = 19-May-2015 14:42:57
datetime.setDefaultFormats('defaultdate','yyyy-MM-dd'); datetime
ans = 19-May-2015 14:43:05
Peter Perkins
Peter Perkins 2015 年 5 月 26 日
From the help:
"datetime.setDefaultFormats('default', FMT) sets the default display format for datetimes. The format is specified by the format string FMT.
datetime.setDefaultFormats('defaultdate', FMT) sets the default display format for datetimes that are created without time components. The format is specified by the format string FMT."
You might want:
>> datetime.setDefaultFormats('default','yyyy-MM-dd'); datetime
ans =
2015-05-26
But never showing the time for datetimes that are not at midnight might not be a good idea. Perhaps
>> datetime.setDefaultFormats('defaultdate','yyyy-MM-dd'); datetime('today')
ans =
2015-05-26
is a more relevant example.

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


Peter Perkins
Peter Perkins 2015 年 5 月 18 日
As Walter says, date is from the set of older functions that include datenum, datestr, and datevec. datetime was introduced in R2014b. Best to not mix the old with the new.
With datetime, there's no worries about converting back and forth between a numeric, a char, and a vector date/time representation. It's all in one place: datetime arrays.
Hope this helps.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by