0

I'm trying to parse a string into a DateTime in flutter. On android, iOS and Web the procedure works perfectly, but on windows it keeps returning, only at midnight, the wrong time.

DateFormat('HH:mm').parse('00:25');

returns 1970-01-01 01:25:00.000 on windows, but 1970-01-01 00:25:00.000 on any other OS, while any hour >= 1 returns the right value.

If I try using parseStrict method, the following happens

DateFormat('HH:mm').parseStrict('00:25');

returns FormatException: Error parsing 00:25, invalid hour value: 0 in it with time zone offset 1:00:00.000000. Expected value between 1 and 1. Date parsed as 1970-01-01 01:25:00.000..

Thanks in advance for any help

Edit: This DateTime is in local timezone, I'm not trying to parse a UTC time. I've noticed, though, that if I add the date, the DateTime seems to get parsed properly. But I need only the time. These values are the open hours for a shop. I know that I SHOULD use TimeOfDay class, but I can't use it because it doesn't have the compareTo method, which I need for my case scenario. Anyway, I've tried Android, iOS and Web and they all parse the time properly, the only one problematic is Windows

  • have you tried with parseUTC, it seems like it is because of time zones – Luis A. Chaglla Apr 29 '21 at 17:48
  • Hello, thank you for your answer. This DateTime is in local timezone, I'm not trying to parse a UTC time. I've noticed, though, that if I add the date, the DateTime seems to get parsed properly. But I need only the time. These values are the open hours for a shop. I know that I SHOULD use TimeOfDay class, but I can't use it because it doesn't have the compareTo method, which I need for my case scenario. Anyways, I've tried Android, iOS and Web and they all parse the time properly, the only one problematic is Windows – Daniele Oliva Apr 30 '21 at 07:08
  • "I know that I SHOULD use TimeOfDay class, but I can't use it because it doesn't have the compareTo method" ... Typically things like `List.sort` take a comparison callback for types that don't implement `Comparable`. If you can't use a callback, you could make your own class around `TimeOfDay` (whether via inheritance or composition) that implements `Comparable`. Other alternatives are to [parse the time yourself](https://stackoverflow.com/a/61394854/) or to internally prepend some dummy date string before parsing. `DateFormat.parse` isn't really meant to parse just times. – jamesdlin Apr 30 '21 at 07:44

0 Answers0