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
}
有许多格式化选项。从文档中可以看出:
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
import 'package:intl/intl.dart';
main() {
var formattedDate = new DateTime.Format('yyyy-MM-dd').DateTime.now();
print(formattedDate); // something like 2020-04-16
}
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
}
结果:
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
String dateConverter(String date) {
// Input date Format
final format = DateFormat("dd-MM-yyyy");
DateTime gettingDate = format.parse(date);
final DateFormat formatter = DateFormat('yyyy-MM-dd');
// Output Date Format
final String formatted = formatter.format(gettingDate);
return date;
}
void main() {
final DateTime now = DateTime.now();
final DateFormat format = DateFormat('yyyy-MM-dd');
final String formatted = format.format(now);
// 2021-03-02
}
/// 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 must be yyyy-MM-dd HH:mm
/// If 27/05/2021 9:34:12.781341 then format must 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();
}
dateTimeFormet (date){
//MM-dd-yyyy
//yyyy-MM-dd
return DateFormat('dd-MM-yyyy').format(date);// you can set your formet
}
void main (){
var date = 01-11-2022 00 : 00
var _datetime = dateTimeFormet(date);
print(_dateTime);
}