Asked by Eric Metzger
on 24 Sep 2018

I have data I need to ingest that is date specific however, the data is represented as a scaler e.g. 20180924 with no spaces or hyphens. How do I get datenum to take this date in and separate it out into a vector so I can use it?

Answer by Kelly Kearney
on 24 Sep 2018

Accepted Answer

Is your final goal to convert to a datenumber or an year-month-day array? As mentioned in the other answers, if you're using a recent version of Matlab, datetimes are more flexible than datenumbers. Once you've converted the value to a datetime, you can move between datenumbers and datevectors pretty easily:

x = [20180924];

>> t = datetime(num2str(x, '%08d'), 'inputFormat', 'yyyyMMdd')

t =

datetime

24-Sep-2018

>> datenum(t)

ans =

737327

>> datevec(t)

ans =

2018 9 24 0 0 0

Eric Metzger
on 24 Sep 2018

Do have one question though, what does the "%08d" do and "inputFormat"?

Stephen Cobeldick
on 24 Sep 2018

@Eric Metzger: read about the formatSpec:

and inputFormat:

Kelly Kearney
on 24 Sep 2018

Sign in to comment.

Answer by ANKUR KUMAR
on 24 Sep 2018

If A is numeric, then

A=20180924

a1=num2str(A)

datenum(str2double({a1(1:4),a1(5:6),a1(7:8)}));

If you wish to store date in vector, then

A1=[str2double({a1(1:4),a1(5:6),a1(7:8)})]

ANKUR KUMAR
on 24 Sep 2018

"I want to store it as a vector [2018 09 24]"

You can never store 09 as a matrix element because it reads as 09 as 9. But if you want to store as 09, then you must to move to class char.

Stephen Cobeldick
on 24 Sep 2018

Note that the square brackets around str2double are totally superfluous:

Eric Metzger
on 24 Sep 2018

Okay, this will break it up into three separate cell, but I get this error when I try to use datenum on it:

A1 =

1×3 cell array

{'2018'} {'09'} {'24'}

>> datenum(A1) Error using datenum (line 189) DATENUM failed.

Caused by: Error using datevec (line 281) Cannot parse date 2018.

I am not use to working with the {} brackets for arrays/matrices but the [] e.g [2018 9 24 18 15 56] where it is the yyyy mm dd hh mm ss. The "three cell array" is not working the way I normally use it. This is close to not quite the cigar yet.

Sign in to comment.

Answer by Walter Roberson
on 24 Sep 2018

datetime(TheScalar, 'ConvertFrom', 'yyyymmdd')

You can convert the result to datenum if you insist: just double() the datetime

Walter Roberson
on 24 Sep 2018

Year=floor(TheScalar /10000);

Day=mod(TheScalar, 100);

Month=mod((TheScalar - Day) / 100, 100);

Steven Lord
on 24 Sep 2018

I would use the ymd function or the datevec function on the datetime object.

theScalar = 20180924

dt = datetime(theScalar, 'ConvertFrom', 'yyyymmdd')

[y, m, d] = ymd(dt)

DV = datevec(dt)

You can even go from the date vector back to a datetime.

dt2 = datetime(DV)

isequal(dt, dt2)

Walter Roberson
on 24 Sep 2018

Sign in to comment.

Answer by Peter Perkins
on 1 Oct 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/420479-how-to-use-datenum-with-a-date-represented-as-a-scaler#comment_613654

## Eric Metzger (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/420479-how-to-use-datenum-with-a-date-represented-as-a-scaler#comment_613660

## Stephen Cobeldick (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/420479-how-to-use-datenum-with-a-date-represented-as-a-scaler#comment_613677

Sign in to comment.