美文网首页Flutter
Flutter(三十)DateTime

Flutter(三十)DateTime

作者: 天色将变 | 来源:发表于2019-07-19 08:03 被阅读2次

    获取时间

    常用属性

    DateTime time = DateTime.now();

    • time.day,今天多少号
    • time.month 当前月份
    • time.year
    • time.hour
    • time.minute
    • time.second
    • time.millisecond
    • time.millisecondsSinceEpoch
    常用方法

    DateTime last = DateTime.now();

    • print(time.difference(last)>Duration(seconds: 1));// 比较二者差异
    • time.add(Duration(seconds: 1));
    • print(time.compareTo(last));//大于返回1
    • print(time.isAfter(last));//是否在之后
    • print(time.isBefore(last));//是否在之前
    • print(time.isAtSameMomentAs(last));//是否在同一时刻
    image.png
    class _MyHomePageState extends State<MyHomePage> {
      DateTime time;
      DateTime last;
      @override
      void initState() {
        super.initState();
        last = DateTime.now();
      }
      @override
      Widget build(BuildContext context) {
    
        time = DateTime.now();
        print(time.difference(last)>Duration(seconds: 1));// 比较二者差异
        time.add(Duration(seconds: 1));
        print(time.compareTo(last));//大于返回1
        print(time.isAfter(last));//是否在之后
        print(time.isBefore(last));//是否在之前
        print(time.isAtSameMomentAs(last));//是否在同一时刻
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Column(
            children: <Widget>[
              Text('time.day='+time.day.toString()),
              Text('time.month='+time.month.toString()),
              Text('time.year='+time.year.toString()),
              Text('time.hour='+time.hour.toString()),
              Text('time.minute='+time.minute.toString()),
              Text('time.second='+time.second.toString()),
              Text('time.millisecond='+time.millisecond.toString()),
              Text('time.millisecondsSinceEpoch='+time.millisecondsSinceEpoch.toString()),
            ],
          ),
        );
      }
    }
    

    输出:

    I/flutter ( 2341): false
    I/flutter ( 2341): 1
    I/flutter ( 2341): true
    I/flutter ( 2341): false
    I/flutter ( 2341): false
    

    相关文章

      网友评论

        本文标题:Flutter(三十)DateTime

        本文链接:https://www.haomeiwen.com/subject/pybdlctx.html