Search code examples
regexflutterdatetimedartstring-formatting

Finding time and date with Regular Expression (RegEx) in Dart language


I'm writing an application with Flutter. I read the times and dates from a source. The date and time format string sent by the resource is:

(Day, Month, Year, Hour, Minute, Second)

07.04.2021 13:30:00
03.04.2021 11:30:00
04.04.2021 17:30:00
03.04.2021 17:30:00

I want to convert this date and time format to DateTime data type with DateTime.parse() function. Here are some examples of what this function accepts as strings and what I need:

"2012-02-27 13:27:00"
"20120227 13:27:00"
"20120227T132700"

I have to convert the string type data coming to me from the source into one of these formats. But in Dart language I couldn't create the Regular Expression needed to do this and couldn't find it anywhere.

I would be very grateful if anyeone could help me understand what I should do.


Solution

  • If you have to play a lot with the dates, you could use the Jiffy package to ease your development.

    DateTime yourDatetime = Jiffy("07.04.2021 13:30:00", "dd.MM.yyyy hh:mm:ss").dateTime;