[英]Fetch date from String and convert to Date object in Flutter Dart
我搜索了很多,但找不到一个好的解决方案。 实际上,知道 Javascript 代码来解决我的问题。 但我在 Dart 中尝试了不同的方法。 我的问题是我正在通过 API 获取时间表。 我得到的回应是
05:30 (PTC)
13:10 (PTC)
17:20 (PTC)
我需要得到这个
5:30 AM
1:10 PM
5:20 PM
我在 Javascript 中尝试和工作的代码
function tConvert (time) {
time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) { // If time format correct
time = time.slice (1); // Remove full string match value
time[5] = +time[0] < 12 ? ' AM' : ' PM'; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join (''); // return adjusted time or original string
}
//called as
$("#time").html(tConvert(time));
这里还有一个问题,他的 (PTC) 有时会更改为其他代码,例如 (BST) (IST) 等。所以只需替换(PTC 不起作用)。
经过长时间的搜索,我解决了这些问题。 所以我想分享我的代码。 感谢@hiwa Jalal 提供帮助。
采取了以下措施:
String removeABC = removebrace2.replaceAll(RegExp(r'[a-zA-Z]'), '');
DateTime gotFinalTime = intl.DateFormat('hh:mm').parse(removeABC);
String formattedTime = intl.DateFormat.jm().format(gotFinalTime);
print(formattedTime);
这对我有用。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.