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

ParameterOptional/RequiredDescription
noneno parameters requiredthis 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,

  1. We create a DateTime object now using DateTime.now() to get the current date and time.
  2. We then access the day property of now to get the day of the month.
  3. 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,

  1. We create a DateTime object date with a specific date, such as October 15, 2023.
  2. We access the day property of date to get the day of the month.
  3. 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,

  1. We create a DateTime object birthday by parsing a date string, such as '2000-05-03'.
  2. We use the day property of birthday to get the day of the month.
  3. 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.