如何将包含 GMT 的字符串时间转换为日期时间

huangapple go评论52阅读模式
英文:

How to convert String time to datetime which includes gmt

问题

应该以这种格式发送时间

"2023-01-09T10:45:00.000+05:30"

我要解析成日期时间的字符串只是 "10:00 AM"
我尝试了以下方法 
```dart
 var GMTdateFormat = DateFormat('yyyy-MM-ddHH:mm:ss.000+05:30').format(
        DateTime.parse(
          (state as SlotRequestPopUpDataLoadedState)
              .slotStartTimingList
              .where((element) => element.isSelected == true)
              .map((e) => e.name)
              .toString(),
        ).toUtc(),
      );

但是没有生效


<details>
<summary>英文:</summary>

Time should be sent in this format 

"2023-01-09T10:45:00.000+05:30"

and the String which i have to parse into datetime is only &quot;10:00 AM&quot;
i tried doing this 

var GMTdateFormat = DateFormat('yyyy-MM-ddHH:mm:ss.000+05:30').format(
DateTime.parse(
(state as SlotRequestPopUpDataLoadedState)
.slotStartTimingList
.where((element) => element.isSelected == true)
.map((e) => e.name)
.toString(),
).toUtc(),
);

But it is not working

</details>


# 答案1
**得分**: 0

尝试这个:

```dart
// 输入: 10:00 AM
final time = DateFormat('hh:mm a').parse("10:00 AM");
final year = DateTime.now().year;
final month = DateTime.now().month;
final day = DateTime.now().day;
final dateString = DateFormat('$year-$month-${day}THH:mm:ss.SSS+05:30').format(time);
print(dateString); //2023-1-9T10:00:00.000+05:30

注意:以上代码部分没有进行中文翻译,仅提供原始英文代码。

英文:

try this:

//input: 10:00 AM
final time = DateFormat(&#39;hh:mm a&#39;).parse(&quot;10:00 AM&quot;);
final year = DateTime.now().year;
final month = DateTime.now().month;
final day = DateTime.now().day;
final dateString = DateFormat(&#39;$year-$month-${day}THH:mm:ss.SSS+05:30&#39;).format(time);
print(dateString); //2023-1-9T10:00:00.000+05:30

huangapple
  • 本文由 发表于 2023年1月9日 14:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定