一伯襟、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);
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者