Create a monthly datetime sequence

9 views (last 30 days)
Hello everyone,
Can someone please me explain why this code
a=datetime(1981,1,1);
b=datetime(2019,31,1);
t1=a:calmonths(1):b;
t2=datetime(t1,'Format','yyyy-M-d');
M=datevec(t2);
Gives me monthly dates until july 2021? Am I missing something?
Thank you!
  1 Comment
Stephen23
Stephen23 on 12 Mar 2021
t1.Format = 'yyyy-M-d' % the correct way to set the format.
t1 = 1×457 datetime array
1981-1-1 1981-2-1 1981-3-1 1981-4-1 1981-5-1 1981-6-1 1981-7-1 1981-8-1 1981-9-1 1981-10-1 1981-11-1 1981-12-1 1982-1-1 1982-2-1 1982-3-1 1982-4-1 1982-5-1 1982-6-1 1982-7-1 1982-8-1 1982-9-1 1982-10-1 1982-11-1 1982-12-1 1983-1-1 1983-2-1 1983-3-1 1983-4-1 1983-5-1 1983-6-1 1983-7-1 1983-8-1 1983-9-1 1983-10-1 1983-11-1 1983-12-1 1984-1-1 1984-2-1 1984-3-1 1984-4-1 1984-5-1 1984-6-1 1984-7-1 1984-8-1 1984-9-1 1984-10-1 1984-11-1 1984-12-1 1985-1-1 1985-2-1 1985-3-1 1985-4-1 1985-5-1 1985-6-1 1985-7-1 1985-8-1 1985-9-1 1985-10-1 1985-11-1 1985-12-1 1986-1-1 1986-2-1 1986-3-1 1986-4-1 1986-5-1 1986-6-1 1986-7-1 1986-8-1 1986-9-1 1986-10-1 1986-11-1 1986-12-1 1987-1-1 1987-2-1 1987-3-1 1987-4-1 1987-5-1 1987-6-1 1987-7-1 1987-8-1 1987-9-1 1987-10-1 1987-11-1 1987-12-1 1988-1-1 1988-2-1 1988-3-1 1988-4-1 1988-5-1 1988-6-1 1988-7-1 1988-8-1 1988-9-1 1988-10-1 1988-11-1 1988-12-1 1989-1-1 1989-2-1 1989-3-1 1989-4-1 1989-5-1 1989-6-1 1989-7-1 1989-8-1 1989-9-1 1989-10-1 1989-11-1 1989-12-1 1990-1-1 1990-2-1 1990-3-1 1990-4-1 1990-5-1 1990-6-1 1990-7-1 1990-8-1 1990-9-1 1990-10-1 1990-11-1 1990-12-1 1991-1-1 1991-2-1 1991-3-1 1991-4-1 1991-5-1 1991-6-1 1991-7-1 1991-8-1 1991-9-1 1991-10-1 1991-11-1 1991-12-1 1992-1-1 1992-2-1 1992-3-1 1992-4-1 1992-5-1 1992-6-1 1992-7-1 1992-8-1 1992-9-1 1992-10-1 1992-11-1 1992-12-1 1993-1-1 1993-2-1 1993-3-1 1993-4-1 1993-5-1 1993-6-1 1993-7-1 1993-8-1 1993-9-1 1993-10-1 1993-11-1 1993-12-1 1994-1-1 1994-2-1 1994-3-1 1994-4-1 1994-5-1 1994-6-1 1994-7-1 1994-8-1 1994-9-1 1994-10-1 1994-11-1 1994-12-1 1995-1-1 1995-2-1 1995-3-1 1995-4-1 1995-5-1 1995-6-1 1995-7-1 1995-8-1 1995-9-1 1995-10-1 1995-11-1 1995-12-1 1996-1-1 1996-2-1 1996-3-1 1996-4-1 1996-5-1 1996-6-1 1996-7-1 1996-8-1 1996-9-1 1996-10-1 1996-11-1 1996-12-1 1997-1-1 1997-2-1 1997-3-1 1997-4-1 1997-5-1 1997-6-1 1997-7-1 1997-8-1 1997-9-1 1997-10-1 1997-11-1 1997-12-1 1998-1-1 1998-2-1 1998-3-1 1998-4-1 1998-5-1 1998-6-1 1998-7-1 1998-8-1 1998-9-1 1998-10-1 1998-11-1 1998-12-1 1999-1-1 1999-2-1 1999-3-1 1999-4-1 1999-5-1 1999-6-1 1999-7-1 1999-8-1 1999-9-1 1999-10-1 1999-11-1 1999-12-1 2000-1-1 2000-2-1 2000-3-1 2000-4-1 2000-5-1 2000-6-1 2000-7-1 2000-8-1 2000-9-1 2000-10-1 2000-11-1 2000-12-1 2001-1-1 2001-2-1 2001-3-1 2001-4-1 2001-5-1 2001-6-1 2001-7-1 2001-8-1 2001-9-1 2001-10-1 2001-11-1 2001-12-1 2002-1-1 2002-2-1 2002-3-1 2002-4-1 2002-5-1 2002-6-1 2002-7-1 2002-8-1 2002-9-1 2002-10-1 2002-11-1 2002-12-1 2003-1-1 2003-2-1 2003-3-1 2003-4-1 2003-5-1 2003-6-1 2003-7-1 2003-8-1 2003-9-1 2003-10-1 2003-11-1 2003-12-1 2004-1-1 2004-2-1 2004-3-1 2004-4-1 2004-5-1 2004-6-1 2004-7-1 2004-8-1 2004-9-1 2004-10-1 2004-11-1 2004-12-1 2005-1-1 2005-2-1 2005-3-1 2005-4-1 2005-5-1 2005-6-1 2005-7-1 2005-8-1 2005-9-1 2005-10-1 2005-11-1 2005-12-1 2006-1-1 2006-2-1 2006-3-1 2006-4-1 2006-5-1 2006-6-1 2006-7-1 2006-8-1 2006-9-1 2006-10-1 2006-11-1 2006-12-1 2007-1-1 2007-2-1 2007-3-1 2007-4-1 2007-5-1 2007-6-1 2007-7-1 2007-8-1 2007-9-1 2007-10-1 2007-11-1 2007-12-1 2008-1-1 2008-2-1 2008-3-1 2008-4-1 2008-5-1 2008-6-1 2008-7-1 2008-8-1 2008-9-1 2008-10-1 2008-11-1 2008-12-1 2009-1-1 2009-2-1 2009-3-1 2009-4-1 2009-5-1 2009-6-1 2009-7-1 2009-8-1 2009-9-1 2009-10-1 2009-11-1 2009-12-1 2010-1-1 2010-2-1 2010-3-1 2010-4-1 2010-5-1 2010-6-1 2010-7-1 2010-8-1 2010-9-1 2010-10-1 2010-11-1 2010-12-1 2011-1-1 2011-2-1 2011-3-1 2011-4-1 2011-5-1 2011-6-1 2011-7-1 2011-8-1 2011-9-1 2011-10-1 2011-11-1 2011-12-1 2012-1-1 2012-2-1 2012-3-1 2012-4-1 2012-5-1 2012-6-1 2012-7-1 2012-8-1 2012-9-1 2012-10-1 2012-11-1 2012-12-1 2013-1-1 2013-2-1 2013-3-1 2013-4-1 2013-5-1 2013-6-1 2013-7-1 2013-8-1 2013-9-1 2013-10-1 2013-11-1 2013-12-1 2014-1-1 2014-2-1 2014-3-1 2014-4-1 2014-5-1 2014-6-1 2014-7-1 2014-8-1 2014-9-1 2014-10-1 2014-11-1 2014-12-1 2015-1-1 2015-2-1 2015-3-1 2015-4-1 2015-5-1 2015-6-1 2015-7-1 2015-8-1 2015-9-1 2015-10-1 2015-11-1 2015-12-1 2016-1-1 2016-2-1 2016-3-1 2016-4-1 2016-5-1 2016-6-1 2016-7-1 2016-8-1 2016-9-1 2016-10-1 2016-11-1 2016-12-1 2017-1-1 2017-2-1 2017-3-1 2017-4-1 2017-5-1 2017-6-1 2017-7-1 2017-8-1 2017-9-1 2017-10-1 2017-11-1 2017-12-1 2018-1-1 2018-2-1 2018-3-1 2018-4-1 2018-5-1 2018-6-1 2018-7-1 2018-8-1 2018-9-1 2018-10-1 2018-11-1 2018-12-1 2019-1-1

Sign in to comment.

Accepted Answer

David Hill
David Hill on 12 Mar 2021
a=datetime(1981,1,1);
b=datetime(2019,1,31);%must be year,month,day
t1=a:calmonths(1):b;
t2=datetime(t1,'Format','yyyy-M-d');
M=datevec(t2);
  1 Comment
Enrico Gambini
Enrico Gambini on 12 Mar 2021
Omg i'm so stupid.... Thank you....

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!