MATLAB Answers

converting yyyymmdd date to matlab date number

95 views (last 30 days)
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?

Accepted Answer

Andrew Newell
Andrew Newell on 26 Jul 2011
For each date, use a command like this:
datestr(datenum('19940127','yyyymmdd'),'mm/dd/yyyy')
  2 Comments
Peter Perkins
Peter Perkins on 2 Apr 2019
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.

Sign in to comment.

More Answers (2)

Jan
Jan on 27 Jul 2011
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 on 1 Apr 2019
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')

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by