Dart – Parse custom DateTime string to DateTime

We often use intl package to format DateTime. It provides many built-in formats (called “Skeletons”) that help us save time to write our own patterns. You can find those skeletons here.

...
/// Skeletons: These can be specified either as the ICU constant name or as the
/// skeleton to which it resolves. The supported set of skeletons is as follows.
/// For each skeleton there is a named constructor that can be used to create
/// it.  It's also possible to pass the skeleton as a string, but the
/// constructor is preferred.
///
///      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
///
/// Examples Using the US Locale:
///
///      Pattern                           Result
///      ----------------                  -------
///      new DateFormat.yMd()             -> 7/10/1996
///      new DateFormat('yMd')            -> 7/10/1996
///      new DateFormat.yMMMMd('en_US')   -> July 10, 1996
///      new DateFormat.jm()              -> 5:08 PM
///      new DateFormat.yMd().add_jm()    -> 7/10/1996 5:08 PM
///      new DateFormat.Hm()              -> 17:08 // force 24 hour time
///
...


However, those skeletons do not always match our need. Sometimes we need to parse the date time in a custom format. In that case, we will have to write our “Explicit Pattern Syntax”. We have to follow below rules when writing the pattern:

...
/// 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
///     z        time zone              (Text)             Pacific Standard Time
///     Z        time zone (RFC 822)    (Number)           -0800
///     v        time zone (generic)    (Text)             Pacific Time
///     Q        quarter                (Text)             Q3
///     '        escape for text        (Delimiter)        'Date='
///     ''       single quote           (Literal)          'o''clock'
///
...


We may have to come back and look at this table (cheatsheet) later when we want to parse a specific custom date time. Below is an example that demonstrates how to use above table.

import 'package:intl/intl.dart';

void main() {
  final dateStr = 'August 6, 2020 at 5:44:45 PM UTC+7';
  final formatter = DateFormat(r'''MMMM dd, yyyy 'at' hh:mm:ss a Z''');

  final dateTimeFromStr = formatter.parse(dateStr);
  print(dateTimeFromStr);
}


Output

2020-08-06 17:44:45.000
Tagged : / / /
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x