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
nowusingDateTime.now()to get the current date and time. - We then access the
dayproperty ofnowto 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
datewith a specific date, such asOctober 15, 2023. - We access the
dayproperty ofdateto 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
birthdayby parsing a date string, such as'2000-05-03'. - We use the
dayproperty ofbirthdayto 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.