toYYYYMMDD method

String toYYYYMMDD()

Formats the DateTime object to a string in the format yyyy-MM-dd.

Example:

DateTime now = DateTime.now();
String formattedDate = now.toYYYYMMDD();
print(formattedDate); // Output: 2024-04-14

Implementation

String toYYYYMMDD() {
  final month = this.month.toString().padLeft(2, '0');
  final day = this.day.toString().padLeft(2, '0');
  final year = this.year.toString();
  return '$year-$month-$day';
}