3

The format of "new DateTime.now()" will print following output:

2015-05-20 07:34:43.018

By having only the time as a String in the correct format ("07:34:43.018"), how do I parse the time to a DateTime object? The usage of the intl package does not support the mentioned format AFAIK.

tosh
  • 5,222
  • 2
  • 28
  • 34
Ed Michel
  • 898
  • 1
  • 11
  • 23

1 Answers1

3

By prefixing the Date in front of the time, DateTime would be able to parse the given String.

DateTime parse(String timeValue) {
   var prefix  = '0000-01-01T';
   return DateTime.parse(prefix + timeValue);
}

If you also only want to display the time afterwards, format your DateTime variable accordingly:

String format(DateTime value) {
   return "${value.hour}:${value.minute}:${value.second}.${value.millisecond}";
}

I've mainly had to parse the String for calculations (difference) with other time values.

tosh
  • 5,222
  • 2
  • 28
  • 34
Ed Michel
  • 898
  • 1
  • 11
  • 23