In this example, we are going to show you the easiest way to format date time in your desired pattern. You can format date and time in different combinations mixed with year, day, weekday, month, month name, hour, minute, second, and many more.
First, add, intl Flutter package in your project by adding the following lines in pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
intl: ^0.17.0
DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2023-01-16 20:04:17.118089
DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2023-01-16 20:04:17.118089
String datetime1 = DateFormat("yyyy-MM-dd").format(datetime);
print(datetime1);
//output: 2023-01-16
String datetime2 = DateFormat.Hms().format(datetime);
print(datetime2);
//output (Time): 20:04:17
String datetime3 = DateFormat.MMMMEEEEd().format(datetime);
print(datetime3);
//output: Sunday, October 16
DateFormat() is from intl package.
DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2021-10-17 20:04:17.118089
String year = datetime.year.toString();
print(year); //output: 2021
String month = datetime.month.toString();
print(month); //Output: 10
String hour = datetime.hour.toString();
print(hour); //output (24 hour format): 20
String minute = datetime.minute.toString();
print(minute);
String second = datetime.second.toString();
print(second);
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:flutter/material.dart';
import 'package:intl/intl.dart';
void main() {
runApp(
MaterialApp(
home: MyApp()
)
);
}
class MyApp extends StatefulWidget{
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2021-10-17 20:04:17.118089
String datetime1 = DateFormat("yyyy-MM-dd").format(datetime);
print(datetime1);
//output: 2021-10-17
String datetime2 = DateFormat.Hms().format(datetime);
print(datetime2);
//output (Time): 20:04:17
String datetime3 = DateFormat.MMMMEEEEd().format(datetime);
print(datetime3);
//output: Sunday, October 17
String datetime4 = DateFormat(DateFormat.YEAR_ABBR_MONTH_WEEKDAY_DAY).format(datetime);
print(datetime4);
//output: Sun, Oct 17, 2021
String year = datetime.year.toString();
print(year); //output: 2021
String month = datetime.month.toString();
print(month); //Output: 10
String hour = datetime.hour.toString();
print(hour); //output (24 hour format): 20
String minute = datetime.minute.toString();
print(minute);
String second = datetime.second.toString();
print(second);
return Scaffold(
appBar: AppBar(
title:Text("Format Date and Time"), //appbar title
backgroundColor: Colors.redAccent //appbar background color
),
body: Container(
alignment:Alignment.topCenter,
padding: EdgeInsets.all(35),
child: Column(
children:[
Text(datetime1),
Text(datetime2),
Text(datetime3),
Text(datetime4),
]
)
)
);
}
}
In this way, you can format the date and time in Dart and Flutter.
Please Wait...
No any Comments on this Article