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
This line in your second example inputs the month as the minutes.
ReplyDeleteprint(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.
Yes, You are right. Thanks for correction.
ReplyDeleteGood job!! Thank you
ReplyDeleteWelcome :)
DeleteThank you. Was helpful for me.
ReplyDeleteYou are welcome
DeleteGood Flutter Knowledge
ReplyDeleteString date = '2000-1-22';
ReplyDeleteI am using this date but your code is not working
String date = Sep 13, 1993
ReplyDeleteI must say that overall I am really impressed with this blog. It is easy to see that you are impassioned about your writing.
ReplyDeleteDapps Development Company
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.
ReplyDeleteWith over a decade of experience in creating mobility solutions, JPloft offers robust and natively compiled experiences for web, mobile, and desktop.