Up: datetime Class   [Contents]

4.1.1 Datenum Compatibility

While the underlying data representation of datetime is compatible with (in fact, identical to) that of datenums, you cannot directly combine them via assignment, concatenation, or most arithmetic operations.

This is because of the signature of the datetime constructor. When combining objects and primitive types like double, the primitive type is promoted to an object by calling the other object’s one-argument constructor on it. However, the one-argument numeric-input consstructor for datetime does not accept datenums: it interprets its input as datevecs instead. This is due to a design decision on Matlab’s part; for compatibility, Octave does not alter that interface.

To combine datetimes with datenums, you can convert the datenums to datetimes by calling datetime.ofDatenum or datetime(x, 'ConvertFrom', 'datenum'), or you can convert the datetimes to datenums by accessing its dnums field with x.dnums.


dt = datetime('2011-03-04')
dn = datenum('2017-01-01')
[dt dn]
    ⇒ error: datenum: expected date vector containing [YEAR, MONTH, DAY, HOUR, MINUTE, SECOND]
[dt datetime.ofDatenum(dn)]
    ⇒ 04-Mar-2011   01-Jan-2017

Also, if you have a zoned datetime, you can’t combine it with a datenum, because datenums do not carry time zone information.