AI.*_*AI. 4 datetime dart flutter
如何将时间转换为 am/pm ?
我有这个输出
I/flutter (17720): 9:00:00
I/flutter (17720): 11:00:00
I/flutter (17720): 12:00:00
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的
final item = snapshot.data[index];
print("Time " + item['time'].toString());
DateTime dateTime = DateTime.parse(item['time'].toString());
print(DateUtil().formattedTime(dateTime));
Run Code Online (Sandbox Code Playgroud)
日期工具
String formattedTime(DateTime dateTime) {
return DateFormat().add_jm().format(dateTime);
}
Run Code Online (Sandbox Code Playgroud)
错误
I/flutter (17720): 时间 09:00:00
????????? 小部件库捕获的异常 ??????????????????????????????????????? 以下 FormatException 被抛出 building Tags(dirty, state: TagsState#b3a2f): Invalid date format 09:00:00
小智 15
使用此代码:
DateFormat('hh:mm a').format(DateTime.now());
Run Code Online (Sandbox Code Playgroud)
根据intl库,它指出 a 代表 AM/PM。
Sik*_*jan 12
您可以使用国际库https://pub.dev/packages/intl 并格式化您的 DateTime DateFormat.yMEd().add_jms().format(DateTime.now()); ==> '星期四,2013 年 5 月 23 日上午 10:21:47'
ANK*_*OJA 12
DateFormat 用于以区域设置敏感的方式格式化和解析日期。
转换时间
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse("14:15:00")));
Run Code Online (Sandbox Code Playgroud)
输出:凌晨 3:20
转换日期
print(DateFormat('yyyy-MMMM-dd').format("2021-05-14 00:00:00.000"));
Run Code Online (Sandbox Code Playgroud)
输出: 2021 年 5 月 14 日
时间选择器
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _con.selectedTime,
);
if (picked != null) {
String selTime =
picked.hour.toString() + ':' + picked.minute.toString() + ':00';
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse(selTime)));
}}
Run Code Online (Sandbox Code Playgroud)
日期选择器
_selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: _con.selectDateAndTime,
firstDate: DateTime(2021),
lastDate: DateTime(2040),
);
if (picked != null) {
print(picked);
}}
Run Code Online (Sandbox Code Playgroud)
此功能可以帮助仅转换时间,不转换日期
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
String formatedTime(TimeOfDay selectedTime) {
DateTime tempDate = DateFormat.Hms().parse(selectedTime.hour.toString() +
":" +
selectedTime.minute.toString() +
":" +
'0' +
":" +
'0');
var dateFormat = DateFormat("h:mm a");
return (dateFormat.format(tempDate));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9028 次 |
| 最近记录: |