How to convert String to Date and Date To String in Flutter.


MorUde Frrr - (Fly Or Not)


Many developer has question like how to convert String to Date or Date to String in Flutter? I’m gonna explain you both.

First we need to add new package date_format in “pubspec.yaml” file. Below is link that will help you for more information.











x


After installing the “date_format” package you have to import package in you .dart file.


import 'package:date_format/date_format.dart';

1)Convert Date to String

void convertStringFromDate() {
  final todayDate = DateTime.now();
  print(formatDate(todayDate, [yyyy, '-', mm, '-', dd, ' ', hh, ':', nn, ':', ss, ' ', am]));
}
     
Call above function by convertStringFromDate() that will print date that you given format. Also check your self by changing “yyyy to yy/ mm to MM or m/ dd to d” etc.
2)Convert String to Date

void convertDateFromString(String strDate){
  DateTime todayDate = DateTime.parse(strDate);
  print(todayDate);
  print(formatDate(todayDate, [yyyy, '/', mm, '/', dd, ' ', hh, ':', nn, ':', ss, ' ', am]));
}
  

Call above function by convertDateFromString('2018-09-27 13:27:00') and output will be flutter: 2018/09/27 01:09:00 PM