Flutter Datetime month value

417

Import intl package to use various date formats

import 'package:intl/intl.dart';

Then use 'MM' to get date as 06:

DateTime tarih=DateTime.now();
String ay = DateFormat('MM').format(tarih);
print("ay:$ay"); //Prints 06
print("tarih:$tarih");

It also supports other formats

DateFormat('EEE d MMM').format(tarih) //Returns Sat 20 Jun
DateFormat('dd/MM/YY').format(tarih) //Returns 20/06/20
Share:
417
Mustafa
Author by

Mustafa

Updated on December 21, 2022

Comments

  • Mustafa
    Mustafa over 1 year

    I want to print the month value like 06 but it writing 6. How solve this problem.

    DateTime tarih=DateTime.now();
    String ay=tarih.month.toString();
    print("ay:$ay");
    print("tarih:$tarih");
    

    I/flutter (17892): ay:6

    I/flutter (17892): tarih:2020-06-20 13:10:51.425817