hour
Hour component of input date and time
Syntax
Description
Examples
t = datetime('today'):hours(8):datetime('tomorrow'); t.Format = 'MMM dd, HH:mm'
t = 1×4 datetime
Jan 24, 00:00 Jan 24, 08:00 Jan 24, 16:00 Jan 25, 00:00
h = hour(t)
h = 1×4
0 8 16 0
Input Arguments
Input date and time, specified as a datetime
array.
To support existing code that previously required Financial Toolbox™, hour also accepts serial date numbers
and text as inputs, but they are not recommended. For more information, see
Version
History.
Extended Capabilities
The
hour function fully supports tall arrays. For more information,
see Tall Arrays.
Usage notes and limitations:
In generated code, this function supports only
datetimearrays as inputs. You cannot specify serial date numbers or text as inputs in generated code.
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation.
The hour function fully supports
thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The hour function fully supports
distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2014bAlthough hour supports serial date number and text inputs,
datetime values are recommended instead. The
datetime 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"); h = hour(t)
h =
15
There are no plans to remove support for serial date number or text inputs.
In previous releases, there were two hour 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 hour without Financial Toolbox. When you call hour from MATLAB, it accepts all previous input types.
For example, you can make these calls with a serial date number and text from MATLAB:
h = hour(738427.656845093)
h =
15
h = hour("2021/09/28 15:45:51.4160")
h =
15
In addition, hour accepts a second input for the date format
when the first input is not a datetime array:
h = hour(t,F)
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:
h = hour("2021/09/28 15:45:51.4160","yyyy/dd/mm hh:MM:ss.fff")
h =
15
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)