MATLAB: Conversion of dates to a different format

conversiondates

Hi,
I need to convert 20110530 (2011 May 30) to 2011150 (which is the 150th day since January 01, 2011). How can i perform this task in MATLAB?

Best Answer

The new date and time functions probably have built-in functions to do what you want, but in their absence, this works:
t_str = '20110530';
dn = datenum('20110530', 'yyyymmdd');
dnstart = datenum(dt_str(1:4), 'yyyy'); % Start Of 2011
dayofyear = dn-dnstart+1
Result = sprintf('%4s%03d', dt_str(1:4),dayofyear)
Result =
2011150
The +1 is necessary for ‘dayofyear’ because it computes the difference between 01-Jan-2011 and doesn’t include 01-Jan-2011.
EDIT — Added ‘Result’ assignment.