码迷,mamicode.com
首页 > 其他好文 > 详细

Flutter DateTime日期转换

时间:2019-11-08 12:38:19      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:size   相等   本地   ssi   sig   rom   字符   print   定时   

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}‘);

 

 

Flutter DateTime日期转换

标签:size   相等   本地   ssi   sig   rom   字符   print   定时   

原文地址:https://www.cnblogs.com/www3j/p/11818972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!