Trying to convert 6 digit number to a date

조회 수: 3 (최근 30일)
Simeon
Simeon 2024년 9월 5일
댓글: Walter Roberson 2024년 9월 5일
I have a table of values such as '060724' that I want to convert to dates so that I can then sort them by date but when I use datetime() I can't seem to get it to work. So for the example above I would want it to say '6th July 2024' so is there a way to do it? My code is
date = 060724
dates(x,:) = datetime(date,'Locale','en_GB','InputFormat','D-MM-uu');

채택된 답변

Shivam
Shivam 2024년 9월 5일
편집: Shivam 2024년 9월 5일
Hi Simeon,
The format 'D-MM-uu' you are using won't work because it doesn't match the structure of your input string. Instead, you should use 'ddMMyy' as the InputFormat since your string represents the day, month, and year without any delimiters.
% Example date string
dateStr = '060724';
% Convert to datetime
dateObj = datetime(dateStr, 'InputFormat', 'ddMMyy', 'Format', 'd MMMM yyyy');
% Display the result
disp(dateObj);
6 July 2024
You can visit the documentation of datetime to know more:
Hope it helps.
  댓글 수: 2
Simeon
Simeon 2024년 9월 5일
Brilliant thanks
Walter Roberson
Walter Roberson 2024년 9월 5일
Note too that the input to datetime() is a character vector (or string) rather than numeric.
datetime() does have the ability to convert some kinds of numeric values, using the ConvertFrom option, but mmddyy is not one of the supported options. (yyyymmdd is a supported option though.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by