Dart 日期时间 DateTime

  var now = new DateTime.now();//获取当前时间 返回:2021-10-11 18:12:28.261102
  var now1 =new DateTime.utc(1969, 7, 20, 20, 18, 04);//世界标准时间 返回:1969-07-20 20:18:04.000Z
    // int month = 1,  年
    // int day = 1, 日
    // int hour = 0, 时
    // int minute = 0, 分
    // int second = 0, 秒
    // int millisecond = 0, 毫秒
    // int microsecond = 0 微秒
now.compareTo(DateTime.april);相比于//将此 DateTime 对象与 [other] 进行比较,如果值相等则返回零。如果此 DateTime [isBefore] [other],则返回负值。如果它 [isAtSameMomentAs] [other],则返回 0,否则返回正值(当 this [isAfter] [other] 时)。
now.difference(other)差异
差别
区别
不同
返回 [Duration] 与从 [this] 减去 [other] 时的差值。如果 [other] 出现在 [this] 之后,则返回的 [Duration] 将为负数。 dart var berlinWallFell = DateTime.utc(1989, DateTime.november, 9); var dDay = DateTime.utc(1944, DateTime.june, 6);持续时间差 = berlinWallFell.difference(dDay);断言(difference.inDays == 16592); 差异以秒和秒的分数来衡量。上面的差异计算这些日期开始时午夜之间的小数秒数。如果上述日期是当地时间,而不是 UTC,则由于夏令时差异,两个午夜之间的差异可能不是 24 小时的倍数。例如,在澳大利亚,类似的代码使用本地时间而不是 UTC:dart var berlinWallFell = DateTime(1989, DateTime.november, 9); var dDay = DateTime(1944, DateTime.june, 6);持续时间差 = berlinWallFell.difference(dDay);断言(difference.inDays == 16592); 会失败,因为差异实际上是 16591 天和 23 小时,而 [Duration.inDays] 只返回整天数。
now.isAfter(other)isAfter
是后
是之后
是后的
如果 [this] 出现在 [other] 之后,则返回 true。比较与时间是 UTC 还是本地时区无关。 “`dart var now = DateTime.now(); var later = now.add(const Duration(seconds: 5));断言(稍后。isAfter(现在)); assert(!now.isBefore(now));即使更改时区,这种关系也保持不变。断言(后来。isAfter(现在。toUtc()));断言(后来。toUtc()。isAfter(现在));断言(!now.toUtc().isBefore(now));断言(!now.isBefore(now.toUtc()));
now.isAtSameMomentAs(other)是在同一时刻作为
是在同一时刻
是在同一时刻的
是在相同的时刻
如果 [this] 与 [other] 发生在同一时刻,则返回 true。比较与时间是 UTC 还是本地时区无关。 `dart var now = DateTime.now(); var later = now.add(const Duration(seconds: 5));断言(!later.isAtSameMomentAs(现在));断言(现在。isAtSameMomentAs(现在));即使更改时区,这种关系也保持不变。断言(!later.isAtSameMomentAs(now.toUtc())); assert(!later.toUtc().isAtSameMomentAs(now));断言(now.toUtc().isAtSameMomentAs(now));断言(now.isAtSameMomentAs(now.toUtc()));
now.isBefore(other)isBefore
是之前
是在
是以前
如果 [this] 出现在 [other] 之前,则返回 true。比较与时间是 UTC 还是本地时区无关。 “`dart var now = DateTime.now(); var early = now.subtract(const Duration(seconds: 5));断言(更早。isBefore(现在)); assert(!now.isBefore(now));即使更改时区,这种关系也保持不变。断言(earlier.isBefore(now.toUtc()));断言(earlier.toUtc().isBefore(now));断言(!now.toUtc().isBefore(now));断言(!now.isBefore(now.toUtc()));
now.subtract(duration)/səbˈtrakt/
动词: 减去, 减, 减掉
返回一个新的 [DateTime] 实例,其中 [duration] 从 [this] 中减去。 今天dart DateTime = DateTime.now();日期时间五十天前 = today.subtract(const Duration(days: 50)); 请注意,减去的持续时间实际上是 50 24 60 60 秒。如果生成的 DateTime 具有与 this 不同的夏令时偏移量,则结果将不会具有与 this 相同的时间,甚至可能不会提前 50 天到达日历日期。处理本地时间的日期时要小心。
时间戳var now = new DateTime.now();

print(now.millisecondsSinceEpoch);//单位毫秒,13位时间戳

print(now.microsecondsSinceEpoch);//单位微秒,16位时间戳
THE END
喜欢就支持一下吧
点赞11赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容