Clear Filters
Clear Filters

How do I align date formats

3 views (last 30 days)
Sebastian
Sebastian on 16 Nov 2016
Answered: Peter Perkins on 18 Nov 2016
Hi, I have some dates I need converted to the same format. I've boiled it down to the following possible formats but now I'm stuck:
01/01/2016
1/01/2016
01/1/2016
1/1/2016
I need them all to be: 01/01/2016
I've tried a few things but nothing works without messing up something else at this point.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Nov 2016
dates = {'01/01/2016', '1/01/2016', '01/1/2016', '1/1/2016'};
datestr(datenum(dates), 'dd/mm/yyyy')

More Answers (1)

Peter Perkins
Peter Perkins on 18 Nov 2016
Using datetimes, Walter's suggestion would look like
>> dates = {'01/01/2016'; '1/01/2016'; '01/1/2016'; '1/1/2016'};
>> datetime(dates,'Format','dd/MM/yyyy')
ans =
01/01/2016
01/01/2016
01/01/2016
01/01/2016

Categories

Find more on Dates and Time in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!