一、DateTime
1、格式化
//引入import 'package:intl/intl.dart';
DateTime date = DateTime(2020, 9, 1);
String dateString = DateFormat("yyyy-MM-dd").format(date).toString();
print(dateString);//2020-09-01
DateTime date = DateTime.now();
String dateString = DateFormat("yyyy-MM-dd HH:mm:ss").format(date).toString();
print(dateString);
2、获取某天的某个时间
//今天的0点
DateTime date = DateTime.parse(DateFormat("yyyy-MM-dd").format(DateTime.now()).toString() + ' ' + "00:00:00");
print(date);//2020-09-05 00:00:00.000
//某天的8点
DateTime targetDate = DateTime(2020, 9, 1);
DateTime date = DateTime.parse(DateFormat("yyyy-MM-dd").format(targetDate).toString() + ' ' + "08:00:00");
print(date);//2020-09-01 08:00:00.000
3、获取某个月份的第一天
DateTime date = DateTime(2020, 9, 5);//或者当前时间DateTime.now();
DateTime firstDay = DateTime(date.year, date.month);
print(firstDay);//2020-09-01 00:00:00.000
4、获取某个月份的最后一天
DateTime date = DateTime(2020, 9, 5); //或者当前时间DateTime.now();
//下个月的第一天
DateTime lastDateTime = DateTime(date.year, date.month + 1);
//再减一天
DateTime lastDay = lastDateTime.subtract(Duration(days: 1));
print(lastDay);//2020-09-30 00:00:00.000
二、String-int-double类型转换
//String -> double
double doubleValue = double.parse('1.2345');
print(doubleValue);
//String -> int
int intValue = int.parse('1');
print(intValue);
//double -> String
String doubleString = double.parse('1.2345').toString();
print(doubleString);
//double -> String 保留2位
String doubleString2 = double.parse('1.2345').toStringAsFixed(2);
print(doubleString2);
//double -> int
int doubleInt = 1.2345.toInt();
print(doubleInt);
//int -> double
double intDouble = 2.toDouble();
print(intDouble);
//int -> String
String intString = 1.toString();
print(intString);