Home > Enterprise >  Insert commas into string flutter
Insert commas into string flutter

Time:03-12

Say I have a string value as: June 22 2022 and I want to add comma with code such that it prints out this value: June 22, 2022. How do I achieve that?

CodePudding user response:

In this particular case, you could use DateTime's DateFormatter:

import 'package:intl/intl.dart';

void main() {
  DateFormat inputFormatter = DateFormat('MMMM dd yyyy');
  DateFormat outputFormatter = DateFormat('MMMM dd, yyyy');
  DateTime date = inputFormatter.parse('February 13 2042');
  String formatted = outputFormatter.format(date);
  print(formatted);
}

Console log

February 13, 2042
  • Related