Current date and time as date vector
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 2 26 11 43 39.922
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 2 26 11 43 39
Test Whether Current Time Occurs During DST
To test whether 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 2 26 11 51 0.18054
tf = logical 0
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.
To return a datetime scalar representing the current date and time, type:
t = datetime('now')
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.