'Insert commas into string flutter

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thierry