(1) Effect picture
(2) Implementation
Modify the pubspec.yaml file
Import the file shown in the red tick, remind everyone, this file cannot have extra spaces, semicolons, etc. or the format is not equal, otherwise it will cause the download dependency to fail
Copy the following code into the main.dart file to run the
code:
import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';//导包
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: FormDemoPage()
);
}
}
class DatePickerDemo extends StatefulWidget {
DatePickerDemo({Key key}) : super(key: key);
@override
_DatePickerDemoState createState() => _DatePickerDemoState();
}
class _DatePickerDemoState extends State<DatePickerDemo> {
var now = DateTime.now();
@override
void initState() {
//TODO: implement initState
super.initState();
//print(now);//打印当前时间 2020-12-29 06:21:34.729165
//print(now.millisecondsSinceEpoch);//时间戳 1609222955510
//print(DateTime.fromMicrosecondsSinceEpoch(1608623002151));//时间戳转换成时间 1970-01-19 14:50:23.002151
print(formatDate(DateTime(2020, 12, 29), [yyyy, '年', mm, '月', dd]));//打印时间 格式
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("datepicker"),
),
body: Text("日期组件"),
);
}
}