基础使用
var now = DateTime.now(); //当前时间
print(now);
//2022-03-10 11:33:11.620721
var firstDay = DateTime(now.year, now.month); //本月第一天
print(firstDay);
//2022-03-01 00:00:00.000
DateTime lastDay = firstDay.subtract(Duration(days: 1)); //减一天,得上个月最后一天
print(lastDay);
//2022-02-28 00:00:00.000
var firstNextMonthDay = DateTime(now.year, now.month + 1); //下月第一天
print(firstNextMonthDay);
//2022-04-01 00:00:00.000
DateTime monthLastDay = firstNextMonthDay.subtract(Duration(days: 1)); //减一天,得本月最后一天
print(monthLastDay);
//2022-03-31 00:00:00.000
var d = DateTime(2019, 1, 10, 9, 30); //指定时间
print(d);
//2019-01-10 09:30:00.000
有时候查询历史记录的时候需要传当天的开始和结束时间
var now = DateTime.now(); //当前时间
var startTime = DateFormat('yyyy-MM-dd_00:00:00').format(now);
var endTime = DateFormat('yyyy-MM-dd_23:59:59').format(now);
print(startTime); //2022-03-10_00:00:00
print(endTime); //2022-03-10_23:59:59