Home > OS >  Format DateTime short date to long date in Flutter
Format DateTime short date to long date in Flutter

Time:09-08

How can I change the date format in Flutter? I want to swap the positions of date and month.

    // prints 2022-09-08 13:46:45.778709
    print('old date');
    print(date);

    // prints September 8, 2022
    print('new date');
    print(DateFormat.yMMMMd('en_US').format(date));
    
    // how to print in 8 September, 2022?
    print('new format');

CodePudding user response:

You can use intl package and do this:

    DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss")
        .parse('2022-09-08 13:46:45.778709');
    String formattedDate = DateFormat('dd MMMM, yyyy').format(tempDate);
    print('formattedDate = $formattedDate'); // 08 September, 2022

CodePudding user response:

If you use the intl package

import 'package:intl/intl.dart';

The following code converts 08/09/2022 15:55 to 09/08/2022 11:55 PM

var inputFormat = DateFormat('dd/MM/yyyy HH:mm');
var inputDate = inputFormat.parse('08/09/2022 15:55'); // <-- dd/MM 24H format
print(inputDate);

var outputFormat = DateFormat('dd MMMM, yyyy');
var outputDate = outputFormat.format(inputDate);
print(outputDate);
  • Related