我找了很多,但没有找到一个好的解决方案.其实,知道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的努力帮助。
采取的步骤是
- 用RegEx删除所有字符串
String removeABC = removebrace2.replaceAll(RegExp(r'[a-zA-Z]'), '');
- 去掉了类似regEx的空格和括号。
- 将我的结果转换为DateFormat
DateTime gotFinalTime = intl.DateFormat('hh:mm').parse(removeABC);
String formattedTime = intl.DateFormat.jm().format(gotFinalTime);
print(formattedTime);
这对我很有效。谅谅