convertTo
Convert datetime
values to numeric representations
Description
X = convertTo(
converts the D
,dateType
)datetime
values in D
to the numeric
representation specified by dateType
and returns a numeric array. For
example, if dateType
is 'posixtime'
, then
convertTo
converts each element of D
to the number
of seconds that have elapsed since the epoch of January 1, 1970,
00:00:00 UTC.
All numeric representations describe dates and times with respect to a point in time called an epoch. Computers typically measure time by the number of seconds or clock ticks since the specified epoch.
X = convertTo(
specifies an epoch and number of clock ticks per second using name-value pair arguments. For
example, if you specify the epoch as D
,'epochtime',Name,Value
)'Epoch','2001-01-01'
, then each
value in X
represents the number of seconds before or after midnight on
January 1, 2001.
Examples
Convert Dates and Times to Excel and .NET Times
Create an array of datetime
values.
D = datetime(2019,10:12,1,12,0,0)
D = 1x3 datetime
01-Oct-2019 12:00:00 01-Nov-2019 12:00:00 01-Dec-2019 12:00:00
Convert D
to an array of double-precision values representing Excel® date numbers. (To display all the digits of the date numbers, set the numeric display format to longg
.)
format longg X = convertTo(D,'excel')
X = 1×3
43739.5 43770.5 43800.5
Convert D
to an array representing dates and times as .NET times. Since .NET times must be unsigned 64-bit integers, convertTo
returns an array having the uint64
data type.
X = convertTo(D,'.net')
X = 1x3 uint64 row vector
637055280000000000 637082064000000000 637107984000000000
Specify Epoch and Clock Ticks
Create a datetime
array. Specify that the time zone is the zone for Tokyo.
D = datetime(2019,10:12,1,12,0,0,'TimeZone','Asia/Tokyo')
D = 1x3 datetime
01-Oct-2019 12:00:00 01-Nov-2019 12:00:00 01-Dec-2019 12:00:00
Convert D
. To represent times as the number of milliseconds that have elapsed since January 1, 2001, specify the Epoch
and TicksPerSecond
name-value pair arguments. You can specify the epoch as a datetime
value or as text formatted to specify a date and time. When you specify 'epochtime'
, the output is an array of signed 64-bit integers.
X = convertTo(D,'epochtime','Epoch','2001-01-01','TicksPerSecond',1000)
X = 1x3 int64 row vector
591624000000 594302400000 596894400000
If the time zone of D
is unspecified, then the convertTo
function treats the elements of D
as UTC times when you specify 'epochtime'
.
Input Arguments
D
— Input dates and times
datetime
array
Input dates and times, specified as a datetime
array.
dateType
— Type of output numeric representation
'posixtime'
| 'excel'
| 'juliandate'
| 'yyyymmdd'
| 'epochtime'
| ...
Type of output numeric representation, specified as a value in the table.
The value of dateType
determines the data type of the output
array. Some numeric representations require unsigned or signed 64-bit integers.
Depending on these requirements, the output can have the double
,
uint64
, or int64
data type.
Value of | Output Numeric Representation | Output Data Type |
---|---|---|
| Number of days since 0-Jan-1900, representing an Excel® date number. This option is equivalent to the Note: Excel date numbers are rounded to the nearest microsecond. Note: Excel incorrectly assumes that the year 1900 is a leap year. Therefore, when computing Excel date numbers there is a discontinuity of one extra day between February 28, 1900 and March 1, 1900. |
|
| Number of days since 0-Jan-1904, representing an Excel date number. This option is equivalent to the Note: Excel date numbers are rounded to the nearest microsecond. Note: Excel incorrectly assumes that the year 1900 is a leap year. Therefore, when computing Excel date numbers there is a discontinuity of one extra day between February 28, 1900 and March 1, 1900. |
|
| Number of days since noon UTC 24-Nov-4714 BCE in the proleptic
Gregorian calendar, representing the Julian Date. This option is equivalent to
the Note: If the time
zone of Example: Convert D.TimeZone = 'Asia/Tokyo'; X = convertTo(D,'juliandate') |
|
| Number of days since midnight UTC 17-Nov-1858, representing the
Modified Julian Date. This option is equivalent to the Note: If the time
zone of Example: Convert D.TimeZone = 'Asia/Tokyo'; X = convertTo(D,'modifiedjuliandate') |
|
| Number of seconds since 1-Jan-1970 00:00:00 UTC, a point in time
known as the Unix epoch. This option is equivalent to the
Unix time, also known as UNIX Epoch time or POSIX time, is the system for measuring time with respect to the Unix epoch. (Unix time ignores leap seconds and therefore does not truly represent UTC.) Note: If the time zone of
Example: Convert D.TimeZone = 'Asia/Tokyo'; X = convertTo(D,'posixtime') |
|
| Dates as YYYYMMDD numeric values. For example,
|
|
| Number of days since 0-Jan-0000 in the proleptic ISO calendar. |
|
| Number of "clock ticks" since 1-Jan-1900 00:00:00 UTC, representing a Network Time Protocol (NTP) timestamp where each clock tick is 2^-32 seconds. |
|
| Number of "clock ticks" since 1-Jan-0001 00:00:00 UTC, representing a Microsoft® .NET timestamp where each clock tick is 100 ns. |
|
| Number of "clock ticks" since 1-Jan-1601 00:00:00 UTC, representing an NTFS timestamp where each clock tick is 100 ns. |
|
| Number of nanoseconds that have elapsed since J2000. The Common Data
Format (CDF) defines the To
convert a Note: J2000 is January 1, 2000, 12:00 Terrestrial Time (TT). TT differs from UTC due to two factors:
As a consequence, J2000 actually occurs 64.184 seconds
before noon in the D = datetime(2000,1,1,11,58,55,816,"TimeZone","UTCLeapSeconds") D = 2000-01-01T11:58:55.816Z Example: Set the time zone of D = datetime(2022,4,1:3,9,30,45, ... "TimeZone","UTCLeapSeconds"); D = D' D =
3x1 datetime array
2022-04-01T09:30:45.000Z
2022-04-02T09:30:45.000Z
2022-04-03T09:30:45.000Z
X = convertTo(D,"tt2000")
X =
3x1 int64 column vector
702077514184000000
702163914184000000
702250314184000000
|
|
| Number of seconds since an epoch that you specify. Use this option
with the Note:
If the time zone of Example: Specify the time zone of
D.TimeZone = 'Asia/Tokyo'; X = convertTo(D,'epochtime','Epoch','2001-01-01') |
|
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: convertTo(D,'epochtime','Epoch','2001-01-01','TicksPerSecond',1000)
returns the number of milliseconds since the beginning of January 1, 2001.
Epoch
— Point used for time measurements
datetime(1970,1,1,0,0,0,'TimeZone','UTC')
(default) | datetime
scalar | character vector | string scalar
Point used for time measurements, specified as the comma-separated pair consisting
of 'Epoch'
and a datetime
scalar, character
vector, or string scalar.
If the value of 'Epoch'
is a character vector or string scalar,
then it must represent a date and time in a format that the
datetime
function recognizes (for example,
'2001-01-01'
representing January 1, 2001).
The default value is the Unix epoch, representing January 1, 1970, 00:00:00 UTC.
TicksPerSecond
— Clock ticks per second
1
(default) | numeric scalar
Clock ticks per second, specified as the comma-separated pair consisting of
'TicksPerSecond'
and a numeric scalar.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced in R2018b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)