To get the time

Commonly used attributes

DateTime time = DateTime.now();

  • Time. day, what’s the date today
  • Time. month Current month
  • time.year
  • time.hour
  • time.minute
  • time.second
  • time.millisecond
  • time.millisecondsSinceEpoch
Commonly used method

DateTime last = DateTime.now();

  • print(time.difference(last)>Duration(seconds: 1)); // Compare the two
  • time.add(Duration(seconds: 1));
  • print(time.compareTo(last)); // greater than returns 1
  • print(time.isAfter(last)); // Whether after
  • print(time.isBefore(last)); // Whether before
  • print(time.isAtSameMomentAs(last)); // Whether at the same time

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)); Print (time.isafter (last)); Print (time.isbefore (last)); Print (time.isatsamemomentas (last)); // Whether that 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()), ], ), ); }}Copy the code

Output:

I/flutter ( 2341): false
I/flutter ( 2341): 1
I/flutter ( 2341): true
I/flutter ( 2341): false
I/flutter ( 2341): false
Copy the code