parse static method
Constructs a new JDate instance based on string
.
The string
must not be null
.
Throws a FormatException if the input string cannot be parsed.
The function parses a subset of ISO 8601 which includes the subset accepted by RFC 3339.
The accepted inputs are currently:
- A date: A signed four-to-six digit year, two digit month and
two digit day, optionally separated by
-
characters. Examples: "13770101", "-0004-12-24", "1600-04-01". - An optional time part, separated from the date by either
T
or a space. The time part is a two digit hour, then optionally a two digit minutes value, then optionally a two digit seconds value, and then optionally a '.' or ',' followed by at least a one digit second fraction. The minutes and seconds may be separated from the previous parts by a ':'. Examples: "12", "12:30:24.124", "12:30:24,124", "123010.50". - An optional time-zone offset part, possibly separated from the previous by a space. The time zone is either 'z' or 'Z', or it is a signed two digit hour part and an optional two digit minute part. The sign must be either "+" or "-", and can not be omitted. The minutes may be separated from the hours by a ':'. Examples: "Z", "-10", "+01:30", "+1130".
This includes the output of both toString and toIso8601String, which
will be parsed back into a DateTime
object with the same time as the
original.
The result is always in either local time or UTC. If a time zone offset other than UTC is specified, the time is converted to the equivalent UTC time.
Examples of accepted strings:
"1387-02-27"
"1387-02-27 13:27:00"
"1387-02-27 13:27:00.123456789z"
"1387-02-27 13:27:00,123456789z"
"13870227 13:27:00"
"13870227T132700"
"13870227"
"+13870227"
"1387-02-27T14Z"
"1387-02-27T14+00:00"
"-123450101 00:00:00 Z"
: in the year -12345."1387-02-27T14:00:00-0500"
: Same as"1387-02-27T19:00:00Z"
Implementation
static JDate parse(String string) {
string = string.numbersToEnglish().replaceAll(RegExp(r'[/\\]'), '-');
final date = DateTime.parse(string);
if (date.isUtc) {
return JDate.utc(
date.year,
date.month,
date.day,
date.hour,
date.minute,
date.second,
date.millisecond,
date.microsecond,
);
} else {
return JDate(
date.year,
date.month,
date.day,
date.hour,
date.minute,
date.second,
date.millisecond,
date.microsecond,
);
}
}