heyr
heyr

Reputation: 5794

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??

Upvotes: 103

Views: 340989

Answers (12)

Dilip
Dilip

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

Ramesh Boosa
Ramesh Boosa

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

TAYAB FAROOQ
TAYAB FAROOQ

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

CopsOnRoad
CopsOnRoad

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

Anit Kumar
Anit Kumar

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

Sandeep Pareek
Sandeep Pareek

Reputation: 1789

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

Upvotes: 3

Ravi Limbani
Ravi Limbani

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

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

SilenceCodder
SilenceCodder

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

Francisco Sales
Francisco Sales

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

vbandrade
vbandrade

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

Vinoth Kumar
Vinoth Kumar

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

Related Questions