如何在Dart中将时间戳字符串从24小时格式转换为12小时格式? [英] How to convert time stamp string from 24 hr format to 12 hr format in dart?

查看:150
本文介绍了如何在Dart中将时间戳字符串从24小时格式转换为12小时格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理flutter应用程序,其中我必须显示时间戳,但是我从api获得的响应是​​24小时格式,并且我想在我的应用程序中以12小时格式显示时间.

我想以这种格式显示在应用程序上 您能帮我解决从24小时到12小时格式化的最简单方法吗?

解决方案

@Umair,正如Sam指出的那样,您可以使用

24小时时间戳字符串

  class MyAppState扩展了State< MyApp>{@override窗口小部件build(BuildContext context){字符串timeStamp24HR ="2020-07-20T18:15:12";返回MaterialApp(家:脚手架(appBar:AppBar(标题:Text('Test'),),身体:填充(填充:EdgeInsets.all(20.0),孩子:中心(子级:Text(新的DateFormat.jm().format(DateTime.parse(timeStamp24HR)),样式:TextStyle(fontSize:18.0),),))));}} 

截屏:

此处提供有关Flutter解析方法的更多信息- https://api.flutter.dev/flutter/dart-core/DateTime/parse.html

I am working on flutter application where i have to show time stamps but the response i got from api is in 24 hr format and i want to display time in 12 hr format in my application.

And i want to display on application in this format Can you please help me regarding the easiest way of doing the formatting from 24 hr to 12 hr?

解决方案

@Umair, as Sam pointed out, you can use intl package and can use jm() function without explicitly declaring the format like so,

For default DateTime

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Padding(
                padding: EdgeInsets.all(20.0),
                child: Center(
                  child: Text(new DateFormat.jm().format(DateTime.now()), style: TextStyle(fontSize: 18.0),),
                )
            )
        ));
  }
}

Screenshot:

For 24 hr timestamp string

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    String timeStamp24HR = "2020-07-20T18:15:12";

    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Padding(
                padding: EdgeInsets.all(20.0),
                child: Center(
                  child: Text(new DateFormat.jm().format(DateTime.parse(timeStamp24HR)), style: TextStyle(fontSize: 18.0),),
                )
            )
        ));
  }
}

Screenshot:

More info on Flutter's parse method here - https://api.flutter.dev/flutter/dart-core/DateTime/parse.html

这篇关于如何在Dart中将时间戳字符串从24小时格式转换为12小时格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆