Main Content

timeofday

Elapsed time since midnight fordatetimearrays

Description

example

T= timeofday(DT)返回一个durationarray whose values equal the elapsed time since midnight for each element inDT.

Fordatetimearrays without time zones, and in most other cases,Tis equal to

E = hours(DT.Hour) + minutes(DT.Minute) + seconds(DT.Second)

IfDThas itsTimeZoneproperty set to a time zone that respects Daylight Saving Time (DST), thentimeofdaytakes DST into account. For more information, seeAlgorithms.

The output argumentTis also equivalent toDT - dateshift(DT,'start','day').

example

[T,D] = timeofday(DT)also returns the date portions of the values inDTas thedatetimearrayD.

The output argumentDis equivalent todateshift(DT,'start','day').

Examples

collapse all

Create adatetimearray.

DT = datetime('now') + hours(1:3)
DT =1x3 datetime26-Feb-2022 13:03:14 26-Feb-2022 14:03:14 26-Feb-2022 15:03:14

Calculate the elapsed time since midnight for each input value. Display the times as hours, minutes, and seconds.

T = timeofday(DT)
T =1x3 duration13:03:14 14:03:14 15:03:14

Create adatetimearray.

DT = datetime('now') + hours(1:3)
DT =1x3 datetime26-Feb-2022 12:42:19 26-Feb-2022 13:42:19 26-Feb-2022 14:42:19

Return the date portions ofDTas a seconddatetimearray. Because the hour, minute, and second components ofDare all set to midnight (or00:00:00in 24-hour format),D只会显示日期。

[T,D] = timeofday(DT)
T =1x3 duration12:42:19 13:42:19 14:42:19
D =1x3 datetime26-Feb-2022 26-Feb-2022 26-Feb-2022

Calculate elapsed times since midnight on a day with a Daylight Saving Time (DST) shift.

Create adatetimearray. Set itsTimeZoneproperty to a time zone that observes DST. Set the date to a day when a DST shift occurred.

tz ='America/New_York'; fmt ='dd-MMM-yyyy HH:mm:ss z'; DT = datetime(2015,3,8,'TimeZone',tz,'Format',fmt) + hours(1:4)
DT =1x4 datetimeColumns 1 through 2 08-Mar-2015 01:00:00 EST 08-Mar-2015 03:00:00 EDT Columns 3 through 4 08-Mar-2015 04:00:00 EDT 08-Mar-2015 05:00:00 EDT

Calculate the elapsed times. The DST shift occurred at 02:00 on March 8, 2015 in this time zone.timeofdayaccounts for the shift for times at or after 02:00 on this date. Set the format ofTto display elapsed times in hours.

T = timeofday(DT); T.Format ='h'
T =1x4 duration1 hr 2 hr 3 hr 4 hr

For comparison, calculate elapsed times using the hour, minute, and second components ofDT. This calculation does not account for the DST shift.

E = hours(DT.Hour) + minutes(DT.Minute) + seconds(DT.Second)
E =1x4 duration1 hr 3 hr 4 hr 5 hr

Set the times of day in adatetimearray according to the times of day in anotherdatetimearray.

There are two ways to set the times of day.

  • Split the time portions from the firstdatetimearray by usingtimeofdayand add the time portions to the seconddatetimearray.

  • Set theHour,Minute, andSecondcomponents of the seconddatetimearray equal to theHour,Minute, andSecondcomponents of the firstdatetimearray.

If you use the first way, you might not always account for Daylight Saving Time (DST) shifts correctly. Only the second way is always correct across any DST shifts.

Create adatetimearray. Each element has a different time component.

DT1 = datetime(2015,3,7) + hours(1:4)
DT1 =1x4 datetimeColumns 1 through 3 07-Mar-2015 01:00:00 07-Mar-2015 02:00:00 07-Mar-2015 03:00:00 Column 4 07-Mar-2015 04:00:00

Create a seconddatetimearray. Each element has the same date and time components.

DT2 = datetime(2015,3,[8 8 8 8])
DT2 =1x4 datetime08-Mar-2015 08-Mar-2015 08-Mar-2015 08-Mar-2015

Set the times of day inDT2according to the times of day inDT1. BecauseDT1andDT2do not have time zones their time components are identical.

DT2 = dateshift(DT2,'start','day') + timeofday(DT1)
DT2 =1x4 datetime列1到3 08 - mar - 2015 01:00:0008-Mar-2015 02:00:00 08-Mar-2015 03:00:00 Column 4 08-Mar-2015 04:00:00

Create adatetimearray with elements that have theTimeZoneproperty set to'America/New_York'. BecauseDT3has a time zone and a DST shift that occurs on March 8, 2015, the time components ofDT3are not the same as those ofDT1.

tz ='America/New_York'; fmt ='dd-MMM-yyyy HH:mm:ss z'; DT3 = datetime(2015,3,8,'TimeZone',tz,'Format',fmt) + timeofday(DT1)
DT3 =1x4 datetimeColumns 1 through 2 08-Mar-2015 01:00:00 EST 08-Mar-2015 03:00:00 EDT Columns 3 through 4 08-Mar-2015 04:00:00 EDT 08-Mar-2015 05:00:00 EDT

Display the elapsed times since midnight.timeofdayaccounts for the DST shift. The elapsed times show that the times inDT3are incorrect. The last time inDT3corresponds to 5:00 a.m. But inDT1only four hours elapsed since midnight.

T = timeofday(DT3)
T =1x4 duration01:00:00 02:00:00 03:00:00 04:00:00

To set the times of day ofDT4correctly regardless of the time zone or the day of year, use theHour,Minute, andSecondproperties ofDT1.

DT4 = datetime(2015,3,[8 8 8 8],'TimeZone',tz,'Format',fmt); DT4.Hour = DT1.Hour; DT4.Minute = DT1.Minute; DT4.Second = DT1.Second; DT4
DT4 =1x4 datetimeColumns 1 through 2 08-Mar-2015 01:00:00 EST 08-Mar-2015 03:00:00 EDT Columns 3 through 4 08-Mar-2015 03:00:00 EDT 08-Mar-2015 04:00:00 EDT

In this time zone, 2:00 a.m. Eastern Standard Time did not exist on March 8, 2015 because the DST shift occurred then. The second element of the result has a time component of 3:00 a.m. Eastern Daylight Time.

Input Arguments

collapse all

Input dates and times, specified as adatetimearray.

Output Arguments

collapse all

Time portions of input array, returned as adurationarray.

Date portions of input array, returned as adatetimearray.

Algorithms

Adatetimearray can have itsTimeZoneproperty set to a time zone that observes Daylight Saving Time (DST). Thetimeofdayfunction takes DST into account.

  • If the input argumentDTis adatetimearray with no time zone, then the outputTis also equal to

    E = hours(DT.Hour) + minutes(DT.Minute) + seconds(DT.Second)

  • IfDThas itsTimeZoneproperty set to a time zone that does not observe DST, thenTis equal toE.

  • IfDThas itsTimeZoneproperty set to a time zone that observes DST, thentimeofdayaccounts for the DST shift on days when the shift occurs. On those days, for times after the DST shift occurs,Tdiffers fromEby the amount of the shift.

Extended Capabilities

Version History

Introduced in R2014b