'How i Can convert 12 hour time string into 24 hour in flutter?
I have a 12 hour formatted String like 02:00 p.m. i want to convert it into 24 hour time format like 14:00:00.
Solution 1:[1]
you can use the intl package https://pub.dev/packages/intl
here is the documentation for the class u need https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
Solution 2:[2]
If you have dateTime or TimeOfDay type you can use intl package, otherwise here is my workaround.
String time12to24Format(String time) {
// var time = "12:01 AM";
int h = int.parse(time.split(":").first);
int m = int.parse(time.split(":").last.split(" ").first);
String meridium = time.split(":").last.split(" ").last.toLowerCase();
if (meridium == "pm") {
if (h != 12) {
h = h + 12;
}
}
if (meridium == "am") {
if (h == 12) {
h = 00;
}
}
String newTime = "${h == 0 ? "00" : h}:${m == 0 ? "00" : m}";
print(newTime);
return newTime;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DZ UY Scuti |
| Solution 2 | Ayyaz meo |
