(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 2022 8 31 3 6 35.858
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 2022 8 31 3 6 35
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 2022 8 31 3 20 0.68
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.
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)