Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how could I convert numeric date into datestr in matlab?

Subject: how could I convert numeric date into datestr in matlab?

From: Skirt Zhang

Date: 20 Jul, 2011 12:16:09

Message: 1 of 5

Dear All,

I have the date which is written in my file as 20091229, of which the class is double. But I want to convert it to datestring 2009-12-29. How could I realize it in matlab?

Subject: how could I convert numeric date into datestr in matlab?

From: Skirt Zhang

Date: 20 Jul, 2011 12:26:08

Message: 2 of 5

"Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message <j06gu9$4s2$1@newscl01ah.mathworks.com>...
> Dear All,
>
> I have the date which is written in my file as 20091229, of which the class is double. But I want to convert it to datestring 2009-12-29. How could I realize it in matlab?

I tried datestr(datenum( 20091229 ))

gives me

ans =

01-Dec-5007

How could I get the true date value?

Subject: how could I convert numeric date into datestr in matlab?

From: Lothar Schmidt

Date: 20 Jul, 2011 12:38:15

Message: 3 of 5

Am 20.07.2011 14:26, schrieb Skirt Zhang:
> "Skirt Zhang" <silence_qunzi@hotmail.com> wrote in message
> <j06gu9$4s2$1@newscl01ah.mathworks.com>...
>> Dear All,
>>
>> I have the date which is written in my file as 20091229, of which the
>> class is double. But I want to convert it to datestring 2009-12-29.
>> How could I realize it in matlab?
>
> I tried datestr(datenum( 20091229 ))
>
> gives me
>
> ans =
>
> 01-Dec-5007
>
> How could I get the true date value?


some kind of stupid brute force segmentation and num2str-conversion:

float_date=20091229;

year=fix(float_date/10000)

remain=float_date-year*10000

month=fix(remain/100)

remain=float_date-year*10000-month*100

day=fix(remain)


date_string=[num2str(year) '-' num2str(month) '-' num2str(day)]

Subject: how could I convert numeric date into datestr in matlab?

From: Bruno Luong

Date: 20 Jul, 2011 12:41:10

Message: 4 of 5

t = 20091229

d=mod(t,100);
t=(t-d)/100;
m=mod(t,100);
y=(t-m)/100;
sprintf('%i-%i-%i', y,m,d)

% Bruno

Subject: how could I convert numeric date into datestr in matlab?

From: Skirt Zhang

Date: 20 Jul, 2011 12:49:09

Message: 5 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j06id6$93v$1@newscl01ah.mathworks.com>...
> t = 20091229
>
> d=mod(t,100);
> t=(t-d)/100;
> m=mod(t,100);
> y=(t-m)/100;
> sprintf('%i-%i-%i', y,m,d)
>
> % Bruno

Thanks a lot Bruno,

I found this might be also OK

p=num2str(20090918)
pp=datenum(p,'yyyymmdd')
then

ppp=datestr(pp)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us