How to convert a string into a date or datenum?
古いコメントを表示
I have a column, about [4000 x 1] with the format 'yyyymmddHH'. Yes, all the numbers are together.
And i need to get a date, or a datenum from that, but i cant, since functions like datetime need the numbers separated.
Anyone know how to get arround this?
Thank you.
回答 (1 件)
"... but i cant, since functions like datetime need the numbers separated."
I don't see that restriction mentioned anywhere in the DATETIME documentation.
S = ["2021061701";"2021010100";"1913061423"]
D = datetime(S, 'InputFormat','yyyyMMddHH')
6 件のコメント
Bruno Carvalho
2021 年 6 月 17 日
If the data are stored as numeric where the digits of one value represent date units (ugh, ugh, ugh, a true abomination and crime against nature) then you can convert to string first:
VeryBadlyStoredDates = [2021061701;2021010100;1913061423] % should be illegal
DT = datetime(string(VeryBadlyStoredDates), 'InputFormat','yyyyMMddHH')
Even better: create or import that data properly as datetime, rather than as numeric.
If you have to operate on the numbers, datetime can handle most of the conversion. You just have to handle the hour data.
VeryBadlyStoredDates = [2021061701;2021010100;1913061423]; % should be illegal
hourData = mod(VeryBadlyStoredDates, 100)
yyyyMMdd = (VeryBadlyStoredDates-hourData)./100
DT = datetime(yyyyMMdd, 'ConvertFrom', 'yyyymmdd') + hours(hourData)
Bruno Carvalho
2021 年 6 月 18 日
Bruno Carvalho
2021 年 6 月 18 日
Stephen23
2021 年 6 月 18 日
@Bruno Carvalho: you can accept this answer, if one of the comments includes the solution.
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!