matlab.datetime.compatibility.convertDatenum
Syntax
Description
Use this function in code where you intend to perform calculations on
datetime
values but, to preserve compatibility, need to interpret text
timestamps in the same way that datenum
interprets them. This function is
designed to be a compatibility layer for use by customers who write functions.
[
additionally returns a logical value that is dt
,wasDatenum
] = matlab.datetime.compatibility.convertDatenum(d
)1
(true
)
if d
is an array of serial date numbers or text timestamps, and
0
(false
) if d
is a
datetime
array.
Examples
Input Arguments
Output Arguments
Tips
To enable code that works with
datetime
arrays to accept text timestamps or serial date numbers as inputs in a backward-compatible way, add a call tomatlab.datetime.compatibility.convertDatenum
at the beginning of your code.For example, if you have defined a function
myFunc
that accepts two input arguments, and the second input argument can be an array of text timestamps, serial date numbers, ordatetime
values, process it by usingmatlab.datetime.compatibility.convertDatenum
. Leave the rest of your code unchanged.function y = myFunc(A,D) D = matlab.datetime.compatibility.convertDatenum(D); <line 1 of original code> <line 2 of original code> ...
In this example,
matlab.datetime.compatibility.convertDatenum
overwrites the input argumentD
. IfD
is already adatetime
array, then it is unaltered.To return an output that matches the data type of the input argument, use the second output of
matlab.datetime.compatibility.convertDatenum
.For example, if you have defined a function
myFunc
that calls thedateshift
function, then you must usedatetime
values in that calculation. But if the input tomyFunc
could be an array of serial date numbers, then you can use the value ofwasDatenum
to decide ifmyFunc
returns an array of serial date numbers or adatetime
array.To convert a
datetime
array to an array of serial date numbers, use theconvertTo
function.function DT = myFunc(D) [D,wasDatenum] = matlab.datetime.compatibility.convertDatenum(D); DT = dateshift(D,"end","month"); if wasDatenum DT = convertTo(DT,"datenum") end end
Version History
Introduced in R2022a