Azzera filtri
Azzera filtri

Create month array?

19 visualizzazioni (ultimi 30 giorni)
Julozert
Julozert il 1 Giu 2020
Modificato: Stephen23 il 4 Nov 2022
Hello,
Is it possible to create a datetime array with just entries from January to December?
I tried it like this but it did not work:
str = (1:12);
t = datetime(str(:),'InputFormat','mm');
I just want an array with 12 entries and each of them is a month with datatype "datetime"

Risposte (2)

Stephen23
Stephen23 il 14 Lug 2021
D = datetime(2021,1,1):calmonths(1):datetime(2021,12,31)
D = 1×12 datetime array
01-Jan-2021 01-Feb-2021 01-Mar-2021 01-Apr-2021 01-May-2021 01-Jun-2021 01-Jul-2021 01-Aug-2021 01-Sep-2021 01-Oct-2021 01-Nov-2021 01-Dec-2021
D.Format = 'MMM'
D = 1×12 datetime array
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  2 Commenti
Ali AJDER
Ali AJDER il 4 Nov 2022
Hello,
Is it possible to create this datetime array independent of the year (i.e. 2021 in this example)?
Thanks.
Stephen23
Stephen23 il 4 Nov 2022
Modificato: Stephen23 il 4 Nov 2022
"Is it possible to create this datetime array independent of the year (i.e. 2021 in this example)?"
The DATETIME object has a YEAR property which always has some value assigned to it: if you do not assign a value to it, then the object construction will use some default value. Also due to the fact that years have different lengths (leap years, leap seconds) the exact time at the start of each month in a year can only be determined once the year has been specified. Because DATETIME objects encode exact times, the DATETIME object must specify the year.
What do you plan on doing with such an array?

Accedi per commentare.


KSSV
KSSV il 14 Lug 2021
year = 2020 ;
t = datetime(year,1,1):dateime(year,12,31) ;

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Tag

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by