Mfj
Mfj

Reputation: 77

How to change my the date format in matlab?

I have

  num4 = xlsread('dat.xlsx', 1, 'A:B');
  dnum4=datetime(num4(:,1),1,1) + caldays(num4(:,2));

dnum4= 
   16-Jul-2008
   18-Jul-2008
   06-Aug-2008
   08-Aug-2008
   13-Aug-2008
   15-Aug-2008
   20-Aug-2008
   22-Aug-2008
   30-Oct-2008

I want to change the outputs from dd-mmm-yyyy to yyyy-mm-dd. How to do that?

Upvotes: 1

Views: 3014

Answers (2)

sco1
sco1

Reputation: 12214

If you look at the documentation you'll see that datetime objects have a Format property that controls the display format:

>> t = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z')

t = 

  datetime

   25-May-2017 10:26:46 -0400


>> t.Format = 'yyyy-MM-dd'

t = 

  datetime

   2017-05-25

Upvotes: 4

Xiangrui Li
Xiangrui Li

Reputation: 2426

One way is to convert to datenum, then back to datestr:

newFmt = datestr(datenum(dnum4, 'dd-mmm-yyyy'), 'yyyy-mm-dd')

Upvotes: -1

Related Questions