Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime to ISO 8601

Tags:

datetime

dart

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.

like image 385
Max Avatar asked Feb 15 '14 15:02

Max


People also ask

How do I format a date in ISO 8601?

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).

Is ISO 8601 always UTC?

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 .


1 Answers

use :

DateTime now = DateTime.now();
String isoDate = now.toIso8601String();
like image 194
Djamel Madani Avatar answered Sep 22 '22 13:09

Djamel Madani