Convert DateTime to ISO 8601

dart, datetime

Solution

use :

DateTime now = DateTime.now();
String isoDate = now.toIso8601String();

Problem

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.

Original source