1.日期转换成时间戳
var now=new DateTime.now();
print(now.millisecondsSinceEpoch); //单位毫秒,13位时间戳
2.时间戳转换成日期
var now=new DateTime.now();
var a=now.millisecondsSinceEpoch; // 时间戳
print(DateTime.fromMillisecondsSinceEpoch(a));
3.创建指定时间
DateTime assignDay = new DateTime(2020,10,10);
print(assignDay); // 2020-10-10 00:00:00.000
4.计算时间跨度
// 例如计算1天14小时45分的跨度
Duration timeRemaining = new Duration(days:1, hours:14, minutes:45);
print(timeRemaining); // 38:45:00.000000
5.字符串转DateTime
DateTime.parse(‘2019-11-08’) 或者 DateTime.parse(‘2019-11-08 12:30:05’)
6.在之前——时间比较
var today = DateTime.now();
var date = DateTime.parse(“2019-06-20 15:32:41”);
today.isBefore(date);
7.在之后——时间比较
var today = DateTime.now();
var date = DateTime.parse(“2019-06-20 15:32:41”);
today.isAfter(date);
8.相等——时间比较
var today = DateTime.now();
var date = DateTime.parse(“2019-06-20 15:32:41”);
today.isAtSameMomentAs(date);
9.时间增加
var today = DateTime.now(); // 2019-11-08 02:54:53.218443
var fiftyDaysFromNow = today.add(new Duration(days: 5));
print(‘today加5天:$fiftyDaysFromNow’); // today加5天:2019-11-13 02:54:53.218443
10.时间减少
var today = DateTime.now(); // 2019-11-08 02:54:53.218443
var fiftyDaysAgo = today.add(new Duration(days: 5));
print('today加5天:$fiftyDaysAgo '); // today减5天:2019-11-03 02:54:53.218443
11.时间差(小时数)
var day1 = new DateTime(2019, 6, 20, 17, 30, 20);
var day2 = new DateTime(2019, 7, 21, 0, 0, 0);
print(‘比较两个时间 差 小时数:${day1.difference(day2)}’); // 比较两个时间 差 小时数:-726:29:40.000000
12.获取年、月、日、星期、时、分、秒、毫秒、微妙
year、month、day、weekday、hour、minute、second、millisecond、microsecond
var today = DateTime.now();
print(today.year);
13.获取本地时区简码
DateTime today = DateTime.now();
print(‘本地时区简码:${today.timeZoneName}’); // 本地时区简码:GMT
14.返回UTC与本地时差(小时数)
DateTime today = DateTime.now();
print(‘返回UTC与本地时差 小时数:${today.timeZoneOffset}’);
旨在分享实用