In Dart, how can I convert a DateTime to its ISO 8601 representation?
Just doing this
var dt = new DateTime.now();
print (dt.toString());
produces
2014-02-15 08:57:47.812
which isn't quite ISO 8601, that would be
2014-02-15T08:57:47.812
so only the "T" is missing - I can easily write my own function for this, just wondering if that already exists somewhere in the core libraries?
The other direction, ISO 8601 to DateTime, is already supported by DateTime.parse.
EDIT looks like the Dart team is adding this now, see here.
ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .
use :
DateTime now = DateTime.now();
String isoDate = now.toIso8601String();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With