(Not recommended; use
datetime) Current date and time as date
clock is not recommended. To return the current date and time as
datetime value, use
datetime instead. For more
information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.
c = clock returns a six-element date vector containing the current date
and time in decimal form:
[year month day hour minute seconds]
clock function calculates the current date and time from
the system time.
[c tf] = clock returns a second output argument that is
true) if the current date and time
occur during daylight saving time (DST) in your system's time zone, and
Round clock Output to Integer Display
To return the current date and time, use the
clock function. Set the output format so that floating-point values display with up to five digits.
format shortg c = clock
c = 1×6 2023 8 19 12 30 46.553
The sixth element of the date vector output (seconds) is accurate to several digits beyond the decimal point.
To round to integer display format, use the
ans = 1×6 2023 8 19 12 30 46
Determine If Current Time Occurs During DST
To determine if the current date and time occur during daylight saving time (DST), use the second output of the
c occurs during DST, and
format shortg [c tf] = clock
c = 1×6 2023 8 19 13 23 5.4249
tf = logical 1
MATLAB® Online™ returns current Coordinated Universal Time (UTC) rather than local time.
To time the duration of an event, use the
tocfunctions instead of
clockfunction is based on the system time, which can be adjusted periodically by the operating system, and thus might not be reliable in time comparison operations.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation is not supported for
clockwith two output arguments.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version HistoryIntroduced before R2006a
R2022b: Not recommended
There are no plans to remove
clock. However, the
datetime function is recommended
datetime data type provides flexible date and time
formats, storage out to nanosecond precision, and properties to account for time
zones and daylight saving time.
To return the current time as a
datetime value, call
datetime. You can call it without any input arguments, or
"now" as the input argument.
d = datetime % or d = datetime("now")
d = datetime 15-Apr-2022 15:53:28
To return the current time as a date vector, split it apart using the
d = datevec(datetime)