Dart DateTime day
Syntax & Examples
Syntax of DateTime.day
The syntax of DateTime.day property is:
int day
This day property of DateTime the day of the month [1..31]
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
none | no parameters required | this property does not take any parameters |
Return Type
DateTime.day returns value of type int
.
✐ Examples
1 Get current day of the month
In this example,
- We create a DateTime object
now
usingDateTime.now()
to get the current date and time. - We then access the
day
property ofnow
to get the day of the month. - We print the result to standard output.
Dart Program
void main() {
DateTime now = DateTime.now();
int dayOfMonth = now.day;
print('Day of the month: $dayOfMonth');
}
Output
Day of the month: 3 (The output may change based on the current day)
2 Get day of the month for a specific date
In this example,
- We create a DateTime object
date
with a specific date, such asOctober 15, 2023
. - We access the
day
property ofdate
to get the day of the month. - We print the result to standard output.
Dart Program
void main() {
DateTime date = DateTime(2023, 10, 15);
int dayOfMonth = date.day;
print('Day of the month: $dayOfMonth');
}
Output
Day of the month: 15
3 Get day of the month for a birthday
In this example,
- We create a DateTime object
birthday
by parsing a date string, such as'2000-05-03'
. - We use the
day
property ofbirthday
to get the day of the month. - We print the result to standard output.
Dart Program
void main() {
DateTime birthday = DateTime.parse('2000-05-03');
int dayOfMonth = birthday.day;
print('Day of the month: $dayOfMonth');
}
Output
Day of the month: 3
Summary
In this Dart tutorial, we learned about day property of DateTime: the syntax and few working examples with output and detailed explanation for each example.