Documentation

timeofday

Elapsed time since midnight for datetimes

Syntax

d = timeofday(t)

Description

example

d = timeofday(t)returns an array of durations equal to the elapsed time since midnight for each of thedatetimevalues int. The output argument,d, is equal to the result oft - dateshift(t,'start','day'), and is the same size ast.

  • If you havedatetimevalues with no time zone, thendalso is equal to

    e = hours(t.Hour) + minutes(t.Minute) + seconds(t.Second)

  • If adatetimevalue has itsTimeZoneproperty set to a time zone that does not observe Daylight Saving Time (DST), thendis equal toe.

  • If adatetimevalue has 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,ddiffers fromeby the amount of the shift.

Examples

collapse all

Create adatetimearray. Calculate the elapsed time since midnight for eachdatetimevalue.

t = datetime('now') + hours(1:3)
t =1×3 datetime array24-Feb-2017 13:31:48 24-Feb-2017 14:31:48 24-Feb-2017 15:31:48
d = timeofday(t)
d =1×3 duration array13:31:48 14:31:48 15:31:48

List the data type ofd. Thetimeofdayfunction returns adurationarray, not adatetimearray.

whosd
Name Size Bytes Class Attributes d 1x3 40 duration

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 date when a DST shift occurred.

tz ='America/New_York'; fmt ='dd-MMM-yyyy HH:mm:ss z'; t = datetime(2015,3,8,'TimeZone',tz,'Format',fmt) + hours(1:4)
t =1×4 datetime array08-Mar-2015 01:00:00 EST 08-Mar-2015 03:00:00 EDT 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.timeofday占时期的转变或者02:00之后on this date.

d = timeofday(t)
d =1×4时间数组01:00:00 02:00:00 03:00:00 04:00:00

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. Only the second method is correct across Daylight Saving Time (DST) shifts.

Create adatetimearray. Each element has a different time component.

t1 = datetime(2015,3,7) + hours(1:4)
t1 =1×4 datetime array07-Mar-2015 01:00:00 07-Mar-2015 02:00:00 07-Mar-2015 03:00:00 07-Mar-2015 04:00:00

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

t2 = datetime(2015,3,[8 8 8 8])
t2 =1×4 datetime array08-Mar-2015 08-Mar-2015 08-Mar-2015 08-Mar-2015

Set the times of day int2according to the times of day int1.

t2 = dateshift(t2,'start','day') + timeofday(t1)
t2 =1×4 datetime array08-Mar-2015 01:00:00 08-Mar-2015 02:00:00 08-Mar-2015 03:00:00 08-Mar-2015 04:00:00

Create adatetimearray with elements that have theTimeZoneproperty set to'America/New_York'.

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

Calculate the elapsed time since midnight.timeofdayaccounts for the DST shift that occurred on March 8, 2015.

d = timeofday(t3)
d =1×4时间数组01:00:00 02:00:00 03:00:00 04:00:00

Set the times of day int4according to times of day int1. To set the times of day correctly regardless of the time zone or the day of year, use theHour,Minute, andSecondproperties oft1.

t4 = datetime(2015,3,[8 8 8 8],'TimeZone',tz,'Format',fmt); t4.Hour = t1.Hour; t4.Minute = t1.Minute; t4.Second = t1.Second; t4
t4 =1×4 datetime array08-Mar-2015 01:00:00 EST 08-Mar-2015 03:00:00 EDT 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 date and time, specified as adatetimearray.

Extended Capabilities

Introduced in R2014b

Was this topic helpful?