To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.
You can use the intl
package (installer) to format dates.
For en_US
formats, it's quite simple:
import 'package:intl/intl.dart';
main() {
final DateTime now = DateTime.now();
final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted); // something like 2013-04-20
}
There are many options for formatting. From the docs:
ICU Name Skeleton
-------- --------
DAY d
ABBR_WEEKDAY E
WEEKDAY EEEE
ABBR_STANDALONE_MONTH LLL
STANDALONE_MONTH LLLL
NUM_MONTH M
NUM_MONTH_DAY Md
NUM_MONTH_WEEKDAY_DAY MEd
ABBR_MONTH MMM
ABBR_MONTH_DAY MMMd
ABBR_MONTH_WEEKDAY_DAY MMMEd
MONTH MMMM
MONTH_DAY MMMMd
MONTH_WEEKDAY_DAY MMMMEEEEd
ABBR_QUARTER QQQ
QUARTER QQQQ
YEAR y
YEAR_NUM_MONTH yM
YEAR_NUM_MONTH_DAY yMd
YEAR_NUM_MONTH_WEEKDAY_DAY yMEd
YEAR_ABBR_MONTH yMMM
YEAR_ABBR_MONTH_DAY yMMMd
YEAR_ABBR_MONTH_WEEKDAY_DAY yMMMEd
YEAR_MONTH yMMMM
YEAR_MONTH_DAY yMMMMd
YEAR_MONTH_WEEKDAY_DAY yMMMMEEEEd
YEAR_ABBR_QUARTER yQQQ
YEAR_QUARTER yQQQQ
HOUR24 H
HOUR24_MINUTE Hm
HOUR24_MINUTE_SECOND Hms
HOUR j
HOUR_MINUTE jm
HOUR_MINUTE_SECOND jms
HOUR_MINUTE_GENERIC_TZ jmv
HOUR_MINUTE_TZ jmz
HOUR_GENERIC_TZ jv
HOUR_TZ jz
MINUTE m
MINUTE_SECOND ms
SECOND s
For non-en_US
dates, you need to explicitly load in the locale. See the DateFormat
docs for more info. The date_symbol_data_local.dart
contains all of the formats for each country/language, if you would like a more in-depth look.
This will work too:
DateTime today = new DateTime.now();
String dateSlug ="${today.year.toString()}-${today.month.toString().padLeft(2,'0')}-${today.day.toString().padLeft(2,'0')}";
print(dateSlug);
You can also specify the date format like stated earlier: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
import 'package:intl/intl.dart';
String formatDate(DateTime date) => new DateFormat("MMMM d").format(date);
Produces: March 4
If someone wants to convert a date in string format to some other string format first use DateTime.parse("2019-09-30") then pass it to DateFormat("date pattern").format() like
dateFormate = DateFormat("dd-MM-yyyy").format(DateTime.parse("2019-09-30"));
Reference: Dart - How to change format of simple date string which is in yyyy-MM-dd to dd-MM-yyyy
pubspec.yaml:
dependencies:
intl:
main.dart:
import 'package:intl/intl.dart'; // for date format
import 'package:intl/date_symbol_data_local.dart'; // for other locales
void main() {
var now = DateTime.now();
print(DateFormat().format(now)); // This will return date using the default locale
print(DateFormat('yyyy-MM-dd hh:mm:ss').format(now));
print(DateFormat.yMMMMd().format(now)); // print long date
print(DateFormat.yMd().format(now)); // print short date
print(DateFormat.jms().format(now)); // print time
initializeDateFormatting('es'); // This will initialize Spanish locale
print(DateFormat.yMMMMd('es').format(now)); // print long date in Spanish format
print(DateFormat.yMd('es').format(now)); // print short date in Spanish format
print(DateFormat.jms('es').format(now)); // print time in Spanish format
}
Result:
May 31, 2020 5:41:42 PM
2020-05-31 05:41:42
May 31, 2020
5/31/2020
5:41:42 PM
31 de mayo de 2020
31/5/2020
17:41:42
This give you the date like in a social network : ["today","yesterday","dayoftheweek",etc..]
void main() {
DateTime now = new DateTime(2018,6,26);
print(date(now));
}
String date(DateTime tm) {
DateTime today = new DateTime.now();
Duration oneDay = new Duration(days: 1);
Duration twoDay = new Duration(days: 2);
Duration oneWeek = new Duration(days: 7);
String month;
switch (tm.month) {
case 1:
month = "january";
break;
case 2:
month = "february";
break;
case 3:
month = "march";
break;
case 4:
month = "april";
break;
case 5:
month = "may";
break;
case 6:
month = "june";
break;
case 7:
month = "july";
break;
case 8:
month = "august";
break;
case 9:
month = "september";
break;
case 10:
month = "october";
break;
case 11:
month = "november";
break;
case 12:
month = "december";
break;
}
Duration difference = today.difference(tm);
if (difference.compareTo(oneDay) < 1) {
return "today";
} else if (difference.compareTo(twoDay) < 1) {
return "yesterday";
} else if (difference.compareTo(oneWeek) < 1) {
switch (tm.weekday) {
case 1:
return "monday";
case 2:
return "tuesday";
case 3:
return "wednesday";
case 4:
return "thursday";
case 5:
return "friday";
case 6:
return "saturday";
case 7:
return "sunday";
}
} else if (tm.year == today.year) {
return '${tm.day} $month';
} else {
return '${tm.day} $month ${tm.year}';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With