datetime

Methods

Method Reference: datetime.convertTo

datetime: X = convertTo (T, dateType)
datetime: X = convertTo (T, 'epochtime', Name, Value)

Convert datetime array to a numeric representation.

X = convertTo (T, dateType) converts the datetime array T to the numeric date/time representation named by dateType, returning an array X of the same size as T. It is a convenience wrapper around the individual conversion methods. dateType may be one of:

  • 'datenum' – serial date number, double (see datenum).
  • 'excel' or 'excel1900' – Excel serial date, 1900 system, double (see exceltime).
  • 'excel1904' – Excel serial date, 1904 system, double.
  • 'juliandate' – Julian date, double (see juliandate).
  • 'modifiedjuliandate' – modified Julian date, double.
  • 'posixtime' – seconds since the Unix epoch, double (see posixtime).
  • 'yyyymmdd'YYYYMMDD numeric date, double (see yyyymmdd).
  • 'epochtime' – ticks since an epoch, int64 (see below).
  • 'ntp' – NTP timestamp, uint64, valid from 1900-01-01 up to 2036-02-07.
  • 'ntfs' – NTFS/FILETIME 100-ns ticks since 1601-01-01, uint64.
  • '.net' – .NET 100-ns ticks since 0001-01-01, uint64.
  • 'tt2000' – CDF TT2000 nanoseconds since the J2000 Terrestrial Time epoch, int64 (see below).

For the double conversions, Not-A-Time (NaT) values are returned as NaN. The integer conversions cannot represent NaN, so a NaT value, an infinite datetime, or a datetime outside the target format’s representable range raises an error.

X = convertTo (T, 'epochtime', Name, Value) accepts the options 'Epoch' (a scalar datetime marking tick zero; default 1970-01-01) and 'TicksPerSecond' (a positive scalar; default 1). The epoch and T must both be zoned or both be unzoned.

X = convertTo (T, 'tt2000') returns the number of nanoseconds since the J2000 Terrestrial Time epoch, 2000-01-01T11:58:55.816Z, as an int64 array. Because that count includes leap seconds, T must be in the 'UTCLeapSeconds' time zone. The inverse is datetime (X, 'ConvertFrom', 'tt2000', 'TimeZone', 'UTCLeapSeconds'), which likewise requires both the int64 type and that zone.

Each conversion treats an inserted leap second the way its own format does. posixtime folds it forward onto the following second, while datenum, exceltime, 'epochtime', 'ntp', 'ntfs' and '.net' fold it backward onto the preceding one; juliandate stretches the day that holds it (see juliandate), and 'tt2000' counts it.

Deviations from MATLAB. Two results differ deliberately for a 'UTCLeapSeconds' array, because MATLAB’s own are inconsistent.

  • 'epochtime' with an 'Epoch': MATLAB folds the array onto the POSIX timeline but measures the epoch on the leap-second one, so its tick count is short by the number of seconds inserted before that epoch. Counting from 2016-12-31 it returns 86373 for 2016-12-31T23:59:59Z, 26 short. Both operands are folded the same way here, giving 86399. Only the offset differs: a difference between two such counts is the same in either.
  • 'tt2000' between 1960 and 1972: before 1972 UTC did not tick with atomic time, and its offset from it is tabulated by the IERS as a base value plus a rate per day. That table is followed here; MATLAB evaluates the rate half a day from where the table places it, leaving its results up to 1.3 milliseconds off. Outside those twelve years the two agree exactly.

Source Code: datetime

convertTo turns a datetime into a numeric date/time representation; one call selects the target system — the MATLAB serial date number, Unix posixtime, the Excel serial date, or a yyyymmdd integer.

 t = datetime (2024, 3, 9, 6, 0, 0)
t =
  datetime

   09-Mar-2024 06:00:00
 convertTo (t, 'datenum')
ans = 7.3932e+05
 convertTo (t, 'posixtime')
ans = 1.7100e+09
 convertTo (t, 'excel')
ans = 4.5360e+04
 convertTo (t, 'yyyymmdd')
ans = 2.0240e+07

The integer epoch systems return exact int64/uint64 values — for example 'epochtime' (seconds since 1970) and 'ntp'.

 t = datetime (2024, 3, 9, 6, 0, 0)
t =
  datetime

   09-Mar-2024 06:00:00
 convertTo (t, 'epochtime')
ans = 1709964000
 convertTo (t, 'ntp')
ans = 16831774110567628800