close

date time format in flutter yyyy-MM-dd hh:mm

Using Intl APIs to format date

Required Imports:


import 'package:intl/intl.dart';  //for date format
import 'package:intl/date_symbol_data_local.dart';  //for date locale

Setting locale:

Naturally ‘Intl’ picks OS neighborhood. Yet, in the event that need to set, It can be set after ways:

Intl.defaultLocale = "zh_HK";   //sets global,
//for specific Format instence only 
var newFormat = new DateFormat.yMMMMd('zh_HK') // pass local, if not passed it will use Intl.defaultLocale, if Intl.defaultLocale not set will use system local.

Formating Date:

  1. Using format string:
var now = new DateTime.now();
var formatter = new DateFormat('yyyy-MM-dd');
String formatted = formatter.format(now);
print(formatted); 

2. Using Various Contructors:

var now = new DateTime.now();
var formatter = new DateFormat.yMMMMd()
String formatted = formatter.format(now);
print(formatted);

3. Create own format:

DateTime today = new DateTime.now();
String dateSlug ="${today.year.toString()}-${today.month.toString().padLeft(2,'0')}-${today.day.toString().padLeft(2,'0')}";
print(dateSlug);

Thanks for being with us on a Flutter Journey!!!

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment