Convert Text into Date Format

5 ビュー (過去 30 日間)
JBA Miller
JBA Miller 2021 年 3 月 1 日
コメント済み: Allen 2021 年 5 月 26 日
Hi, I struggle a lot!
I want to convert the text 196307 into a date format that MATLAB recognizes as "July 1963".
How can I do that?
  1 件のコメント
Allen
Allen 2021 年 5 月 26 日
JBA,
If any of the suggested solutions were able to solve your problem please do not forget to accept the answer. This will help other people that are looking for a similar answer know what works. Otherwise perhaps you can include what problems you may still be running into with the provided solutions.
Thanks,
Allen

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

回答 (3 件)

dpb
dpb 2021 年 3 月 1 日
>> datetime('196307','InputFormat','yyyyMM')
ans =
datetime
01-Jul-1963
>>
or
>> datetime([floor(196307/100) 196307-floor(196307/100)*100 1])
ans =
datetime
01-Jul-1963
>>
Your choice depending upon whether you're starting with a string or a number.

Allen
Allen 2021 年 3 月 1 日
Try the following:
% Code works with either a string or numerical input
str = '196307';
datestr(datenum(str,'yyyymm'),'mmmm yyyy')
num = 196307;
datestr(datenum(num,'yyyymm'),'mmmm yyyy')

Steven Lord
Steven Lord 2021 年 3 月 1 日
If you have a number:
x = 196307;
datetime(100*x+1, 'ConvertFrom', 'yyyymmdd')
ans = datetime
01-Jul-1963
If you have text:
y = string(x);
datetime(y, 'InputFormat', 'yyyyMM')
ans = datetime
01-Jul-1963
  1 件のコメント
dpb
dpb 2021 年 3 月 1 日
datetime(100*x+1, 'ConvertFrom', 'yyyymmdd')
Clever! +1

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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by