Ben Ajax
Ben Ajax

Reputation: 740

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?

Upvotes: 3

Views: 949

Answers (1)

Thierry
Thierry

Reputation: 8393

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

Upvotes: 4

Related Questions