Dart编程语言中的时间日期处理技巧

发表时间: 2021-10-24 18:47

获取当前时间

var now = new DateTime.now();print(now); // 2019-06-20 16:59:05.560543

2、设置时间

var d =new DateTime(2019, 6, 20, 16, 37 , 26);print(d); // 2019-06-20 16:37:26.000

3、创建时间UTC

var ds = new DateTime.utc(2019, 10, 10, 9, 30);print(ds); // 2019-10-10 09:30:00.000Z

4、解析时间

var d1 = DateTime.parse('2018-10-10 09:30:36');print(d1); // 2018-10-10 09:30:36.000var d2 = DateTime.parse('2018-10-10 09:30:30+0800');print(d2); // 2018-10-10 01:30:30.000Z

5、时间加减

print(now.add(new Duration(minutes: 10)));//加10分钟  2019-06-20 17:09:05.560543print(now.add(new Duration(minutes: -10)));//减10分钟 2019-06-20 16:49:05.560543print(now.add(new Duration(hours: 2)));//加2小时 2019-06-20 18:59:05.560543print(now.add(new Duration(hours: -2)));//减2小时 2019-06-20 14:59:05.560543

6、比较时间

var d3 = new DateTime(2019, 6, 20);var d4 = new DateTime(2019, 7, 20);var d5 = new DateTime(2019, 6, 20);print(d3.isAfter(d4));//是否在d4之后 falseprint(d3.isBefore(d4));//是否在d4之前 true print(d3.isAtSameMomentAs(d5));//是否相同 true

7、计算时间差

var d6 = new DateTime(2019, 6, 19, 16 , 30);var d7 = new DateTime(2019, 6, 20, 15, 20);var difference = d7.difference(d6);print([difference.inDays, difference.inHours,difference.inMinutes]);//d6与d7相差的天数与小时,分钟 [0, 22, 1370]

8、时间戳

print(now.millisecondsSinceEpoch);//单位毫秒,13位时间戳  1561021145560print(now.microsecondsSinceEpoch);//单位微秒,16位时间戳  1561021145560543

9、时间格式化

import 'package:intl/intl.dart';main() {  var now = new DateTime.now();  var formatter = new DateFormat('yyyy-MM-dd HH:mm:ss');  String formatted = formatter.format(now);  print(formatted); // 2021-10-20 18:30:}

格式化选项:

 ICU Name                   Skeleton --------                   -------- DAY                          d ABBR_WEEKDAY                 E WEEKDAY                      EEEE ABBR_STANDALONE_MONTH        LLL STANDALONE_MONTH             LLLL NUM_MONTH                    M NUM_MONTH_DAY                Md NUM_MONTH_WEEKDAY_DAY        MEd ABBR_MONTH                   MMM ABBR_MONTH_DAY               MMMd ABBR_MONTH_WEEKDAY_DAY       MMMEd MONTH                        MMMM MONTH_DAY                    MMMMd MONTH_WEEKDAY_DAY            MMMMEEEEd ABBR_QUARTER                 QQQ QUARTER                      QQQQ YEAR                         y YEAR_NUM_MONTH               yM YEAR_NUM_MONTH_DAY           yMd YEAR_NUM_MONTH_WEEKDAY_DAY   yMEd YEAR_ABBR_MONTH              yMMM YEAR_ABBR_MONTH_DAY          yMMMd YEAR_ABBR_MONTH_WEEKDAY_DAY  yMMMEd YEAR_MONTH                   yMMMM YEAR_MONTH_DAY               yMMMMd YEAR_MONTH_WEEKDAY_DAY       yMMMMEEEEd YEAR_ABBR_QUARTER            yQQQ YEAR_QUARTER                 yQQQQ HOUR24                       H HOUR24_MINUTE                Hm HOUR24_MINUTE_SECOND         Hms HOUR                         j HOUR_MINUTE                  jm HOUR_MINUTE_SECOND           jms HOUR_MINUTE_GENERIC_TZ       jmv HOUR_MINUTE_TZ               jmz HOUR_GENERIC_TZ              jv HOUR_TZ                      jz MINUTE                       m MINUTE_SECOND                ms SECOND                       s