0

I have an instance of Date(20201107) and I would like to format that to a String. How do I do that? I want to turn the date into a string, something like "07/11/2020". And I have an instance of Time(103942) become to "10:39:42".

How to implement in widget? thanks a lot.

flutt dev
  • 1
  • 1

1 Answers1

0

You can use a DateFormat, just include intl dependency to your pubspec.yaml

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedDate = DateFormat("dd/MM/yyyy")format(now);
print(formattedDate);

Depending on what your requirements is, you can look at DateFormat

Some examples taken from DateFormat-class to help you a bit more.

String formattedDate = DateFormat.yMd(); // 7/10/1996
String formattedDate = DateFormat("yMd"); // 7/10/1996
String formattedDate = DateFormat.yMMMMd("en_US"); // July 10, 1996

Also see Similar Question

References

Community
  • 1
  • 1
Tinus Jackson
  • 3,397
  • 2
  • 25
  • 58