2022年6月26日星期日

Format DateTime in Flutter + Cheatsheet

 Step 1: Add the intl package.

dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
intl: ^0.17.0 # <-- SEE HERE

Step 2: Get the date that you want to format.

DateTime now = DateTime.now();

Step 3: Use the named constructor and then call the format() method and then provide the date. 

String formattedDate = DateFormat.yMMMEd().format(now);




Custom DateFormat

DateFormat('yyyy-MM-dd hh:mm')

The following characters are available in explicit patterns:

Symbol   Meaning                Presentation       Example
------   -------                ------------       -------
G        era designator         (Text)             AD
y        year                   (Number)           1996
M        month in year          (Text & Number)    July & 07
L        standalone month       (Text & Number)    July & 07
d        day in month           (Number)           10
c        standalone day         (Number)           10
h        hour in am/pm (1~12)   (Number)           12
H        hour in day (0~23)     (Number)           0
m        minute in hour         (Number)           30
s        second in minute       (Number)           55
S        fractional second      (Number)           978
E        day of week            (Text)             Tuesday
D        day in year            (Number)           189
a        am/pm marker           (Text)             PM
k        hour in day (1~24)     (Number)           24
K        hour in am/pm (0~11)   (Number)           0
Q        quarter                (Text)             Q3
'        escape for text        (Delimiter)        'Date='
''       single quote           (Literal)          'o''clock'

沒有留言:

發佈留言