Info

This question is closed. Reopen it to edit or answer.

splitting the date in the same line to three variables

1 view (last 30 days)
Hi,
I am trying to split the date in the same line but still stuggling. Any ideas?
here is my hint:
Thanks!
start_date=20110101
start_date,['%4d',repmat('%2d',1,2)],'start_year','start_month','start_day');
so the result should be:
start_year=2011
start_month=01
start_day=01
  2 Comments
Geoff Hayes
Geoff Hayes on 6 Mar 2019
sensation - what are you trying to do with that second line of code? Is there a particular function that you are calling since there is a trailing ')'? Should your start_year, start_month and start_day be strings or integers (note if integers, you won't be able to prefix it with a zero). Please clarify.

Answers (2)

Star Strider
Star Strider on 6 Mar 2019
Try this:
start_date=20110101;
dva = datevec(num2str(start_date,'%8d'), 'yyyymmdd')
start_year = dva(1)
start_month = dva(2)
start_day = dva(3)
producing:
start_year =
2011
start_month =
1
start_day =
1

Peter Perkins
Peter Perkins on 11 Mar 2019
Depe3nding on whether you have numeric (ads in your OP) or text (as in your comment):
>> start_date = 20110101;
>> d = datetime(start_date,'ConvertFrom','yyyymmdd')
d =
datetime
01-Jan-2011 00:00:00
>> start_date = '20110101';
>> d = datetime(start_date,'InputFormat','yyyyMMdd')
d =
datetime
01-Jan-2011
Not sure why you need to then split this up, you likely do not need to. But if you insist:
>> [yr,mo,day] = ymd(d)
yr =
2011
mo =
1
day =
1

Community Treasure Hunt

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

Start Hunting!