Home > Mobile >  Revert datetime format to be in fu
Revert datetime format to be in fu

Time:11-04

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

CodePudding user response:

 final oldDateDateTime = DateFormat('dd-MM-yy').parse('4-11-22');
 final newDateString = DateFormat('EEEE, d MMMM y', 'en_US').format(oldDateDateTime);

 print(oldDateDateTime.toString());
 print(newDateString);

Output:

2022-11-04 00:00:00.000

Friday, 4 November 2022

More in: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

CodePudding user response:

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.

CodePudding user response:

You can do as follows

    final DateFormat formatter = DateFormat('EEEE, dd MMMM yyyy');
    final String formatted = formatter.format(DateTime.now());

Then you use the formatted

  • Related