方法名称 | 说明 |
---|---|
now() | 命名构造,获取当前时间 |
millisecondsSinceEpoch | DateTime转时间戳 |
fromMillisecondsSinceEpoch | 时间戳转DateTime |
parse(string) | 字符串转DateTime |
isBefore(date) | 时间比较---在之前 |
isAfter(date) | 时间比较---在之后 |
isAtSameMomentAs(date) | 时间比较---相等 |
compareTo(date) | 大于返回1;等于返回0;小于返回-1 |
add(Duration) | 时间增加 |
subtract(Duration) | 时间减少 |
difference(date) | 时间差 两个时间相差 小时数 |
timeZoneName | 本地时区简码 |
timeZoneOffset | 返回UTC与本地时差 小时数 |
year、month、day、hour、minute、second、millisecond、microsecond | 返回 年、月、日、时、分、秒、毫秒、微妙 |
weekday | 返回星期几 |
- CalendarDatePicker 1.5版后面的日期选择
- showDatePicker 弹出日期选择
- showTimePicker 弹出时间选择

demo装了 date_format: ^1.0.8
import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
class DatePickerPage extends StatefulWidget {
DatePickerPage({Key key}) : super(key: key);
@override
_DatePickerPageState createState() => _DatePickerPageState();
}
class _DatePickerPageState extends State<DatePickerPage> {
DateTime today = DateTime.now();
var _time =
TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);
// _showDatePicker() {
// showDatePicker(
// context: context,
// initialDate: today, //初始日期
// firstDate: DateTime(2020, 5, 5),
// lastDate: DateTime(2021, 5, 31)
// ).then((value) {
// setState(() {
// today = value;
// });
// });
// }
_showDatePicker() async {
var _result = await showDatePicker(
context: context,
initialDate: today, //初始日期
firstDate: DateTime(2020, 5, 5),
lastDate: DateTime(2021, 5, 31));
setState(() {
today = _result != null ? _result : today;
});
}
_showTimePicker() {
showTimePicker(context: context, initialTime: _time).then((value) {
setState(() {
print(value);
this._time = value;
print(this._time);
});
});
}
@override
void initState() {
super.initState();
print('当前时间是:$today');
var date1 = today.millisecondsSinceEpoch;
print('当前时间戳:$date1');
var date2 = DateTime.fromMillisecondsSinceEpoch(date1);
print('时间戳转日期:$date2');
//拼接成date
var dentistAppointment = new DateTime(2020, 5, 27, 17, 30, 20);
print(dentistAppointment);
// 字符串转date
DateTime date3 = DateTime.parse("2019-07-29 15:32:41");
print(date3);
// 时间比较
print(today.isBefore(date3)); // 在之前
print(today.isAfter(date3)); // 在之后
print(date3.isAtSameMomentAs(date3)); // 相同
print(date3.compareTo(today)); // 大于返回1;等于返回0;小于返回-1。
// print(DateTime.now().toString());
// print(DateTime.now().toIso8601String());
//时间增加
var fiftyDaysFromNow = today.add(new Duration(days: 5));
print('today加5天:$fiftyDaysFromNow');
//时间减少
DateTime fiftyDaysAgo = today.subtract(new Duration(days: 5));
print('today减5天:$fiftyDaysAgo');
//时间差 两个时间相差 小时数
print('比较两个时间 差 小时数:${fiftyDaysFromNow.difference(fiftyDaysAgo)}');
print('本地时区简码:${today.timeZoneName}');
print('返回UTC与本地时差 小时数:${today.timeZoneOffset}');
print(
'获取年月日:${today.year}'); //month、day、hour、minute、second、millisecond、microsecond
print('星期:${today.weekday}'); // 返回星期几
print(formatDate(today, [yyyy, '-', mm, '-', dd])); //2020-05-19
print(formatDate(
DateTime(2050, 02, 21), [yyyy, '/', mm, '-', dd])); //2050/02-21
print(formatDate(DateTime(1989, 02, 1, 15, 40, 10),
[hh, ':', nn, ':', ss, ' ', am])); // 03:40:10 PM
print(formatDate(DateTime(1989, 02, 1, 15, 40, 10),
[HH, ':', nn, ':', ss, ' ', Z])); //15:40:10 GMT
print(formatDate(DateTime(1989, 3, 2), [yy, '-', mm, '-w', W])); //89-03-w9
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('日期组件 demo')),
body: Column(children: <Widget>[
Column(children: <Widget>[
CalendarDatePicker(
//自带的日期组件
initialDate: today,
onDateChanged: (date) {
setState(() {
today = date;
});
},
firstDate: DateTime(2020, 5, 5),
lastDate: DateTime(2021, 5, 31)),
Text('选择:${formatDate(today, [yyyy, '/', mm, '/', dd])}')
]),
SizedBox(height: 30.0),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Row(children: [
InkWell(
child: Row(children: <Widget>[
Text(formatDate(today, [yyyy, '/', mm, '/', dd])),
Icon(Icons.arrow_drop_down)
]),
onTap: () {
this._showDatePicker();
})
]),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
InkWell(
child: Row(children: <Widget>[
Text('${_time.format(context)}'),
Icon(Icons.arrow_drop_down)
]),
onTap: () {
this._showTimePicker();
})
])
])
]));
}
}
网友评论