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


11 comments:

  1. This line in your second example inputs the month as the minutes.

    print(formatDate(todayDate, [yyyy, '/', mm, '/', dd, ' ', hh, ':', mm, ':', ss, ' ', am]));

    Input Time: 13:27:00
    Output Time: 01:09:00 PM

    I believe you want to use 'nn' instead.


    ReplyDelete
  2. Yes, You are right. Thanks for correction.

    ReplyDelete
  3. Good job!! Thank you

    ReplyDelete
  4. Thank you. Was helpful for me.

    ReplyDelete
  5. String date = '2000-1-22';
    I am using this date but your code is not working

    ReplyDelete
  6. String date = Sep 13, 1993

    ReplyDelete
  7. I must say that overall I am really impressed with this blog. It is easy to see that you are impassioned about your writing.

    Dapps Development Company

    ReplyDelete
  8. If you're looking for the best flutter app development company , your search ends here. JPloft Solution provides the best-in-class, feature-rich cross-platform app solutions based on Flutter technology and the latest trends.

    With over a decade of experience in creating mobility solutions, JPloft offers robust and natively compiled experiences for web, mobile, and desktop.

    ReplyDelete