2

I have a srting date 2019-09-30, I want to convert it to 30-09-2019 in dart

Shashank
  • 895
  • 1
  • 10
  • 20
  • Possible duplicate of [How do I format a date with Dart?](https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart) – George Sep 21 '19 at 13:22
  • Hey Shashank, check out the link above! I guess it could solve your problem better using the intl package with proper functions for date formatting. Good luck :) – George Sep 21 '19 at 13:23
  • @George well I have seen the above answer before asking. The answers given above only formats DateTime.now() but I want to format a string thats' why I asked it here. Fatima 's answer used DateTime.parse to convert string to DateTime that helped me – Shashank Sep 23 '19 at 04:37
  • Alright, no prob :) – George Sep 23 '19 at 20:53

2 Answers2

12

You can use intl.dart to formate any date . Here is a simple example of this:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String dateFormate;
  @override
  Widget build(BuildContext context) {
    dateFormate = DateFormat("dd-MM-yyyy").format(DateTime.parse("2019-09-30"));
    return Container(
      child: Text(dateFormate),
    );
  }
}
Fatima Hossny
  • 1,157
  • 1
  • 13
  • 27
2

Use intl package

This package has a great class called DateFormat that can easily do it for you .

Installing :
Add this to your pubspec.yaml file:

dependencies:
 intl: any

Usage : Just pass your date to DateFormat("date pattern").format(date)

Example

DateFormat("dd-MM-yyyy").format(DateTime.parse("2019-09-30"));

Output : 30-09-2019

Another Example

print(DateFormat.yMd().format(DateTime.now()));

Output : 2/7/2021

This package includes many other tools . You can learn more about them here.

Kab Agouda
  • 6,309
  • 38
  • 32