user34790
user34790

Reputation: 2036

Get all the dates between two dates

I have these two limit days like

2004-01-01
2008-12-09

Now I want all the dates in between them like

2004-01-02
2004-01-03
........
2008-12-08
2008-12-09

How can I do it in matlab?

Upvotes: 0

Views: 1073

Answers (2)

Divakar
Divakar

Reputation: 221754

Use a combination of datestr and datenum -

date1 = '2004-01-01'
date2 = '2008-12-09'
out = datestr(datenum(date1)+1:datenum(date2),'yyyy-mm-dd')

Output -

out =

2004-01-02
2004-01-03
2004-01-04
2004-01-05
..
2008-12-07
2008-12-08
2008-12-09

Upvotes: 2

0x90
0x90

Reputation: 41022

Used this answer for creating the array:

start_date = datenum(2004,1,1);                                   
end_date = datenum(2008,12,9);                                     
interval = datenum(2004,1,2)-start_date ;% 1 day interval
date_range = [start_date:interval:end_date] ; 

For printing it in a readable (UTC) format (convert a serial date number back to a date):

datestr(date_range);

Here is some of the output data:

>> ans(1:365:end,:)

ans =

01-Jan-2004
31-Dec-2004
31-Dec-2005
31-Dec-2006
31-Dec-2007

Upvotes: 2

Related Questions