Reputation: 5794
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
??
Upvotes: 103
Views: 340989
Reputation: 2734
All supportive date formate in flutter you can refer this link flutter. https://api.flutter.dev/flutter/intl/DateFormat-class.html
var inputDateFormat = DateFormat('dd/MM/yyyy HH:mm');
var inputDate = inputDateFormat.parse('25/12/2000 20:50'); // 24Hour format
var outputDateFormat = DateFormat('MM/dd/yyyy hh:mm a');
var outputDate = outputDateFormat.format(inputDate);
print(outputDate); // 12/25/2000 20:50 PM // 24Hour format
Upvotes: -1
Reputation: 198
Just need to follow 5 steps:
Step 1: locate pubspec.yaml file and check dependencies of your application. if it doesn't have intl dependency created yet, make sure to add in your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
intl: ^0.18.0<you can change to desired version>
Step2: Just click CMD+S for MAC OS or Cntrl+S for Windows.
Step 3: observe the bottom toolbar of your editor if you are using VS Code. it will show a message to say it's installing extension
Step 4: Make sure to import the package in your class
import 'package:intl/intl.dart';
Step 5: if you want to display date on Text Widget use below code else just use DateFormatter
Text(DateFormat.yMMMEd().format(item.date)) // this extension gives lots of formats. feel free to use based on your requirement.
Good luck!
Upvotes: 2
Reputation: 31
If DateFormat() is not working in your case you must add "intl" package by running this command on your terminal:
flutter pub get intl
Upvotes: 0
Reputation: 268414
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
Upvotes: 83
Reputation: 8153
/// 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();
}
Upvotes: 9
Reputation: 1789
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
Upvotes: 3
Reputation: 1172
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
Upvotes: 1
Reputation: 59
var dateInFormatText = widget.snapshot["date"].toString().split("/");
DateTime dateResult = new DateTime.utc(
int.parse(dateInFormatText[2]),
int.parse(dateInFormatText[1]),
int.parse(dateInFormatText[0]));
Upvotes: 2
Reputation: 3174
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
Upvotes: 30
Reputation: 51
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
Upvotes: 5
Reputation: 2662
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)));
Upvotes: 138
Reputation: 13471
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])
Upvotes: 18