简体   繁体   中英

How to Convert String to Time in Flutter

I am trying to convert DateTime to Time. I successfully converted it to a String using DateFormat but now i want to convert this String to Time (H,M,S). Is there a way to directly parse and convert the variable?

In general Dart, there is no such thing as a Time object. In Flutter, though, you generally want to represent specific times (without a date component) with TimeOfDay . Unfortunately, it doesn't have a parse method for parsing timestamp strings, so the easiest way to get it is by passing in the DateTime you started with:

TimeOfDay time = TimeOfDay.fromDateTime(myDateTime);

If you also require the seconds component, unfortunately TimeOfDay doesn't support that. In that case, you will either have to create your own Time class or just use the DateTime time components directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM