second - Seconds component of input date and time - MATLAB (original) (raw)
Main Content
Seconds component of input date and time
Syntax
Description
s = second([t](#mw%5Fb13a705a-2422-4e7a-bf50-c670e094263d))
returns the seconds component for each date and time specified in t
. The seconds component of a date and time can include a fractional part.
The s
output is a double
array and contains values from 0 to less than 60. To assign values to the seconds components of values in t
, use t.Second
and modify the Second property.
s = second([t](#mw%5Fb13a705a-2422-4e7a-bf50-c670e094263d),[secondType](#bugbam9-1-secondType))
returns the type of seconds of datetime
arrayt
, specified as either"secondofminute"
or"secondofday"
.
Examples
t1 = datetime('now','Format','dd-MMM-yyyy HH:mm:ss.SSS'); t = t1 + seconds(30:15:60)
t = 1×3 datetime 01-Feb-2025 09:11:46.724 01-Feb-2025 09:12:01.724 01-Feb-2025 09:12:16.724
s = 1×3
46.7243 1.7243 16.7243
Input Arguments
Input date and time, specified as a datetime
array.
If the time zone of t
isUTCLeapSeconds
, then s
can contain a value between 60 and 61 for any time that falls during a leap second occurrence.
To support existing code that previously required Financial Toolbox™, second
also accepts serial date numbers and text as inputs, but they are not recommended. For more information, seeVersion History.
Type of second values, specified as either'secondofminute'
or 'secondofday'
.
- If
secondType
is'secondofminute'
, thensecond
returns the second of the minute. - If
secondType
is'secondofday'
, thensecond
returns the second of the day, which (except for leap seconds) is in the range [0, 86400).
This argument is valid only when t
is adatetime
array.
Extended Capabilities
Thesecond
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2014b
Although second
supports serial date number and text inputs,datetime
values are recommended instead. Thedatetime
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 convert serial date numbers or text to datetime
values, use the datetime function. For example:
t = datetime(738427.656845093,"ConvertFrom","datenum"); s = second(t)
There are no plans to remove support for serial date number or text inputs.
In previous releases, there were two second
functions: one in MATLAB® that accepted datetime
values, and one in Financial Toolbox that accepted serial date numbers or text values. As of R2022a, you can call second
without Financial Toolbox. When you call second
from MATLAB, it accepts all previous input types.
For example, you can make these calls with a serial date number and text from MATLAB:
s = second(738427.656845093)
s = second("2021/09/28 15:45:51.4160")
In addition, second
accepts a second input for the date format when the first input is not a datetime
array:
In this syntax, specify F
using a valid date format as documented for the datestr
function, though'Q'
format specifiers are not supported. For example:
s = second("2021/09/28 15:45:51.4160","yyyy/dd/mm hh:MM:ss.fff")