[英]Fetch date from String and convert to Date object in Flutter Dart
I searched a lot, but couldn't find a good solution.我搜索了很多,但找不到一个好的解决方案。 Actually, know the Javascript code to do my problem.
实际上,知道 Javascript 代码来解决我的问题。 But I tried different methods in Dart.
但我在 Dart 中尝试了不同的方法。 My issue is I am fetching a time table through an API.
我的问题是我正在通过 API 获取时间表。 And I get the response as
我得到的回应是
05:30 (PTC)
13:10 (PTC)
17:20 (PTC)
And I need to get this as我需要得到这个
5:30 AM
1:10 PM
5:20 PM
The code I tried and worked in Javascript我在 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));
One more issue here his (PTC) is sometime changed to other codes like (BST) (IST) etc. So just replacing (PTC don't work).这里还有一个问题,他的 (PTC) 有时会更改为其他代码,例如 (BST) (IST) 等。所以只需替换(PTC 不起作用)。
I solved the issues after a long search.经过长时间的搜索,我解决了这些问题。 So I wished to share my code.
所以我想分享我的代码。 Thanks to @hiwa Jalal for trying to help.
感谢@hiwa Jalal 提供帮助。
Steps were taken:采取了以下措施:
String removeABC = removebrace2.replaceAll(RegExp(r'[a-zA-Z]'), '');
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);
This worked for me.这对我有用。 Thanks
谢谢
add intl to your project将国际添加到您的项目中
then use DateFormat.jm().format(yourTime)
然后使用
DateFormat.jm().format(yourTime)
for more information on DateFormat
class check here有关
DateFormat
class 的更多信息,请查看此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.