Search code examples
flutterdartwebintl

Revert datetime format, from short to long


Tried searching around couldn't really find anything. Was hoping to find a way to revert the datetime format.

So I start off with: 4-11-22 and I want to change to Friday, 12 November 2022.

Using the intl package


Solution

  • import intl package

    import 'package:intl/intl.dart';
    

    Then, you can do as follows:

      String myDate = '4-11-22'; // input date
      String pattern = 'dd-MM-yy'; // define parse pattern for the input date
      DateTime date = DateFormat(pattern).parse(myDate); // parse the input date
    
      String newPattern = 'EEEE, dd MMMM yyyy'; // define new pattern
      String formattedDate = DateFormat(newPattern).format(date); // reformat
      print(formattedDate); // result: Friday, 04 November 2022
    

    Try on DartPad

    For more formatting possibilities, go to the docs.