Main Content

clock

(Not recommended; use datetime) Current date and time as date vector

clock is not recommended. To return the current date and time as a 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.

Description

c = clock returns a six-element date vector containing the current date and time in decimal form:

[year month day hour minute seconds]

The clock function calculates the current date and time from the system time.

example

[c tf] = clock returns a second output argument that is 1 (true) if the current date and time occur during daylight saving time (DST) in your system's time zone, and 0 (false) otherwise.

example

Examples

collapse all

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

         2024            7           20           13           53       40.076

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 fix function.

fix(c)
ans = 1×6

        2024           7          20          13          53          40

To determine if the current date and time occur during daylight saving time (DST), use the second output of the clock function. tf is 1 (true) if c occurs during DST, and 0 (false) otherwise.

format shortg
[c tf] = clock
c = 1×6

         2024            7           20           14           28       8.8319

tf = logical
   1

Limitations

  • MATLAB® Online™ returns current Coordinated Universal Time (UTC) rather than local time.

Tips

  • To time the duration of an event, use the timeit or tic and toc functions instead of clock and etime. The clock function 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.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

| | | | |