How to convert a string to DateTime and DateTime to ISO8601 in Dart

Published by admin on

Dart convert string to datetime to iISO8601

Dart : Converting a string to DateTime and DateTime to ISO8601 string :

In dart, we can easily convert a string to a DateTime object. DateTime.parse() method takes one correctly formatted string as input and convert it to a DateTime object.
Similarly, we can convert a DateTime object to a ISO8601 string using toIso8601String() method. The output is yyyy-MM-ddTHH:mm:ss.mmmuuuZ for UTC time and yyyy-MM-ddTHH:mm:ss.mmmuuu for non-UTC time.

yyyy – Year , from -9999 to 9999
MM – month , from 01 to 12
dd – date, from 01 to 31
HH – hour in 24 hour format, from 00 to 23
mm – minute in range 00 to 59
ss – seconds in range 00 to 59
mmm – milliseconds in the range 000 to 999
uuu – microseconds in the range 001 to 999

We can again parse this string back to a DateTime object using parse() method.

main(List args) {
  DateTime myDatetime = DateTime.parse("2018-07-10 12:04:35");

Output :

Categories: dart


Leave a Reply

Your email address will not be published. Required fields are marked *