Flutter of date, format dates, date picker assembly

Flutter in the date and time stamp

// Get the current date 
DateTime _nowDate = DateTime.now ();
print(_nowDate);//2019-10-29 10:57:20.384872
print (_nowDate.millisecondsSinceEpoch); // time stamp, 1572317840384
print (DateTime.fromMicrosecondsSinceEpoch (1572317840384)); // convert the date stamp, 1970-01-1912: 45: 17.840384

The so-called time stamp refers to the self GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) of the total number of seconds until now .

In some cases, the background might all the time into a timestamp returned to our front-end, which we'll need to convert the timestamp time and time format.

Show time, we will have a variety of forms, such as 1970-01-01,1970 / 01/01, January 1, 1970, and so on, how do we put the same time converted into different formats needed it? Next, I'll introduce a Flutter third-party libraries.

Flutter third-party component libraries

Log pub.dev search date_format see Installing add-dependent component

pubspec.yaml

dependencies:
    date_format: ^1.0.8

Press ctrl + s or flutter packages will automatically get after downloading dependencies, pay attention to the console, without exception is to download success

The introduction of package

import 'package:date_format/date_format.dart';
print(formatDate(DateTime.now(), [yyyy, "-", mm, "-", dd, " ", DD, " ", HH, ":", nn, ":", ss]));

Export

2019-10-29 Wednesday 14:27:29

Call Flutter comes with time and date picker component selector assembly

The method of displaying a calendar component and access to selected data

// _ on behalf of private, private rewrite the calendar established 
  _showDatePicker () {
    showDatePicker(
      context: context, // context must pass 
      initialDate: _nowDate, // set the initialization date 
      firstDate: DateTime (1900), // set the start time 
      lastdate: DateTime (2100), // set the end time 
    ) .then ((val ) { // asynchronous method 
      print (val);
    });
  }

Method Two

_showDatePicker() async{
    val var = await showDatePicker (
      context: context, // context must pass 
      initialDate: _nowDate, // set the initialization date 
      firstDate: the DateTime (1900), // set the start time 
      lastdate: the DateTime (2100), // end time 
    );
    setState(() {
      // will get time to pass variables 
      the this ._nowDate = Val;
    });
  }

Use variable substitution text

Container ( 
margin: EdgeInsets.all (. 5),
width: 350,
height: 120,
Decoration: new new BoxDecoration (
Color: Colors.black12,
borderRadius: BorderRadius.circular (10.0), // border
),
Child: GestureDetector (// gesture event
child: text ( '$ {formatDate (_nowDate, [yyyy, "-", mm, "-", dd])}'), // replacement text
onTap in: () {
_showDatePicker (); // call to heavy write assembly
},
),
),

time

  // own components 
showTimePicker ( context: context, initialTime:
new TimeOfDay.now(), ).then((val) { print(val); }).catchError((err) { print(err); });
  // get the current time 
  var _nowTime = TimeOfDay.now ();


_showTimePicker() async{
    var val = await showTimePicker(
        context: context, // context 
        initialTime: _nowTime // current time, set a fixed time the TimeOfDay (hour: 12 is, minute: 10) 
    );
    setState(() {
      this._nowTime = val;
    });
  }

 

Reference: https://cloud.tencent.com/developer/article/1495839

 

Guess you like

Origin www.cnblogs.com/ssjf/p/11758232.html