How do I convert datetime in string format to Matlab datetime?

1 回表示 (過去 30 日間)
Felicia Ojinji
Felicia Ojinji 2020 年 11 月 16 日
コメント済み: Steven Lord 2020 年 11 月 16 日
I have 8760 rows of data in this format '02/01/2019 09'. I would like to convert this to Matlabe datetime format. Thank you

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 16 日
編集済み: Ameer Hamza 2020 年 11 月 16 日
Try following InputFormat
x = '02/01/2019 09';
dt = datetime(x, 'InputFormat', 'dd/MM/yyyy hh')
Result
>> dt
dt =
datetime
02-Jan-2019 09:00:00
  3 件のコメント
Felicia Ojinji
Felicia Ojinji 2020 年 11 月 16 日
It worked... I added a space before the brackets initially that was why i got the error message. Thanks again Ameer
Steven Lord
Steven Lord 2020 年 11 月 16 日
That text you posted, '02/01/2019 09'. Does it represent January 2nd, 2019 at 9:00 AM? Or does it represent February 1st, 2019 at 9:00 AM? If the latter you want to use MM/dd/yyyy hh'.
s = '02/01/2019 09'
s = '02/01/2019 09'
J = datetime(s, 'InputFormat', 'dd/MM/yyyy hh')
J = datetime
02-Jan-2019 09:00:00
F = datetime(s, 'InputFormat', 'MM/dd/yyyy hh')
F = datetime
01-Feb-2019 09:00:00
Using the wrong format could lead to problems later on if you assumed one of the two potential formats by which s could be interpreted but a later entry is unambiguously in the other format, like:
datetime('20/01/2021', 'InputFormat', 'dd/MM/yyyy')
ans = datetime
20-Jan-2021
datetime('20/01/2021', 'InputFormat', 'MM/dd/yyyy') % Will error
Error using datetime (line 647)
Unable to convert '20/01/2021' to datetime using the format 'MM/dd/yyyy'.

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

その他の回答 (0 件)

カテゴリ

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