converting yyyymmdd date to matlab date number

조회 수: 37 (최근 30일)
joseph Frank
joseph Frank 2011년 7월 26일
댓글: Victor Martinez 2022년 5월 26일
I have a column of dates that have the following format: 19940127 how can i get a matlab number that corresponds to 01/27/1994?

채택된 답변

Andrew Newell
Andrew Newell 2011년 7월 26일
For each date, use a command like this:
datestr(datenum('19940127','yyyymmdd'),'mm/dd/yyyy')
  댓글 수: 2
Elias de Korte
Elias de Korte 2019년 4월 1일
What if I have an time vector that looks like this: 2011021101 (yyyymmddHH). How can i convert this vector to matlab serial numbers?
Peter Perkins
Peter Perkins 2019년 4월 2일
See Steve Lord's answer. If you have a numeric value, you will need to strip off the hours and divide by 100. If you have text, just use a different format - yyyyMMddHH.

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

추가 답변 (2개)

Jan
Jan 2011년 7월 27일
If "19940127" is a string, this is much faster than the DATESTR/DATENUM approach:
S = '19940127';
D = [S(5:6), '/', S(7:8), '/', S(1:4)];
If the input is a number, convert it to a string at first:
N = 19940127;
S = sprintf('%d', N);

Steven Lord
Steven Lord 2019년 4월 1일
Is that stored as a number or as text? If it is stored as a number, use datetime with the appropriate ConvertFrom option.
d = 19940127;
dt = datetime(d, 'ConvertFrom', 'yyyymmdd')
If it is stored as text, specify the InputFormat.
d = '19940127';
dt = datetime(d, 'InputFormat', 'yyyyMMdd')
  댓글 수: 3
Stephen23
Stephen23 2022년 5월 26일
편집: Stephen23 2022년 5월 26일
I would like to know if having my date and time of a whole year stored all together as a numeric value in a column vector (f example: 201801010205 yyyymmddHHMM being that 01-Jan-2018 02:05)"
Best avoided: it would be better if you created or imported that directly as DATETIME, or even as text. Note that functions such as READTABLE provide a lot of control over importing, so there is little reason for the misuse of numeric type like that.
N = 201801010205;
D = datetime(string(N),'inputFormat','yyyyMMddHHmm')
D = datetime
01-Jan-2018 02:05:00
Victor Martinez
Victor Martinez 2022년 5월 26일
Thank you Stephen ! importing the column with the date and time as text instead of number and then converting it to datetime with the proper format as you wrote worked perfectly.
Again, thanks for your time and help.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by