我尝试构建一个应用程序,该应用程序应该给出维也纳当地时间。我格式化了时间time = DateFormat.jm().format(),但现在我得到了一个类似的时间14:25 PM。我怎样才能删除PM,因为我不需要\xc2\xb4t?\n这是我尝试过的代码:
class _MyHomePageState extends State<MyHomePage> {\n void getTime() async {\n String time;\n\n Response response =\n await get("http://worldtimeapi.org/api/timezone/Europe/Vienna");\n Map data = jsonDecode(response.body);\n\n String datetime = data["datetime"];\n String offset = data["utc_offset"].substring(1, 3);\n DateTime now = DateTime.parse(datetime);\n now = now.add(Duration(hours: int.parse(offset)));\n time = DateFormat.jm().format(now);\n print(time);\n }\nRun Code Online (Sandbox Code Playgroud)\n这是我得到的时间:
\nI/flutter ( 9302): 12:43 PM\nRun Code Online (Sandbox Code Playgroud)\n
如果您使用Intl 包,那么您只需提供一个自定义格式化程序就可以做到这一点,而无需太多工作。
对于您的用例,您需要使用HH:mm仅代表小时和分钟的格式化程序。
final date = DateTime.now();
final hourAndMinues = DateFormat('HH:mm').format(date);
print(hourAndMinutes); // 12:00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3808 次 |
| 最近记录: |