英文:
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 "10:00 AM"
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('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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论