Dart DateTime month
Syntax & Examples
Syntax of DateTime.month
The syntax of DateTime.month property is:
int month
This month property of DateTime the month [1..12]
.
Return Type
DateTime.month returns value of type int
.
✐ Examples
1 Get the month of the current DateTime
In this example,
- We create a DateTime object
dt
using theDateTime.now()
constructor, which represents the current date and time. - We then access the
month
property ofdt
to get the month. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.now();
print(dt.month);
}
Output
5 (Output varies depending on the current date and time.)
2 Get the month of a specific DateTime in UTC
In this example,
- We create a DateTime object
dt
using theDateTime.utc()
constructor, specifying the year, month, and day. - We then access the
month
property ofdt
to get the month. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.utc(2024, 10, 3);
print(dt.month);
}
Output
10
3 Get the month of a parsed DateTime
In this example,
- We create a DateTime object
dt
by parsing a date-time string '2024-05-03T10:15:30' using theDateTime.parse()
method. - We then access the
month
property ofdt
to get the month. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.parse('2024-05-03T10:15:30');
print(dt.month);
}
Output
5
Summary
In this Dart tutorial, we learned about month property of DateTime: the syntax and few working examples with output and detailed explanation for each example.