Date Time format in Flutter dd/MM/YYYY hh:mm

My Date-Time format at the moment is this

By using this :

Text(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000).toString(),

I am getting the type of format attached in the picture, however I was wondering if I could get it in dd/MM/YYYY hh:mm??

229592 次浏览

You can use date_format plugin available here https://pub.dartlang.org/packages/date_format

Then to convert,

formatDate(DateTime.now(), [dd, '/', mm, '/', yyyy, ' ', HH, ':', nn])

If you use the intl package

final f = new DateFormat('yyyy-MM-dd hh:mm');


Text(f.format(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000)));

First install the pub.dev/packages/intl package in your pubspec.yaml

   intl: ^0.16.1

Then use

   final df = new DateFormat('dd-MM-yyyy hh:mm a');
int myvalue = 1558432747;
print(df.format(new DateTime.fromMillisecondsSinceEpoch(myvalue*1000)));

Output

21-05-2019 10:59 AM

If you use the intl package:

var date = DateTime.fromMicrosecondsSinceEpoch(miliseconds * 1000)
DateFormat(DateFormat.YEAR_MONTH_DAY, 'pt_Br').format(date.toUtc())

Output: 10 de abril de 2020

var dateInFormatText = widget.snapshot["date"].toString().split("/");


DateTime dateResult = new DateTime.utc(
int.parse(dateInFormatText[2]),
int.parse(dateInFormatText[1]),
int.parse(dateInFormatText[0]));


Use intl package:

import 'package:intl/intl.dart';

The following code converts 31/12/2000 23:59 to 12/31/2000 11:59 PM

var inputFormat = DateFormat('dd/MM/yyyy HH:mm');
var inputDate = inputFormat.parse('31/12/2000 23:59'); // <-- dd/MM 24H format


var outputFormat = DateFormat('MM/dd/yyyy hh:mm a');
var outputDate = outputFormat.format(inputDate);
print(outputDate); // 12/31/2000 11:59 PM <-- MM/dd 12H format

you can use date_format package to format dates in flutter.

import 'package:date_format/date_format.dart';


final formattedStr = formatDate(DateTime.now(), [dd, '/', mm, '/', yyyy, ' ', HH, ':' nn]);


//02-03-2021

try this

Method:

    getFormatedDate(_date) {
var inputFormat = DateFormat('yyyy-MM-dd HH:mm');
var inputDate = inputFormat.parse(_date);
var outputFormat = DateFormat('dd/MM/yyyy');
return outputFormat.format(inputDate);
}

Call:

getFormatedDate(_start_date)// simple date in string format

output:

24/05/2021
/// Get date as a string for display.
String getFormattedDate(String date) {
/// Convert into local date format.
var localDate = DateTime.parse(date).toLocal();


/// inputFormat - format getting from api or other func.
/// e.g If 2021-05-27 9:34:12.781341 then format should be yyyy-MM-dd HH:mm
/// If 27/05/2021 9:34:12.781341 then format should be dd/MM/yyyy HH:mm
var inputFormat = DateFormat('yyyy-MM-dd HH:mm');
var inputDate = inputFormat.parse(localDate.toString());


/// outputFormat - convert into format you want to show.
var outputFormat = DateFormat('dd/MM/yyyy HH:mm');
var outputDate = outputFormat.format(inputDate);


return outputDate.toString();
}

If DateFormat() is not working in your case you must add "intl" package by running this command on your terminal:

flutter pub get intl