美文网首页
flutter时间工具类,获取具体某时间的13位时间戳

flutter时间工具类,获取具体某时间的13位时间戳

作者: 尖叫_ | 来源:发表于2020-09-16 11:21 被阅读0次

获取现在、今天开始、昨天结束、本周开始、本月开始、本年开始的13位时间戳

class TimeUtils {

  ///获取现在的时间
  static int getDayNow(){
    var nowTime = DateTime.now();
    return nowTime.millisecondsSinceEpoch;
  }

  ///获取今天的开始时间
  static int getDayBegin(){
    var nowTime = DateTime.now();
    var day = new DateTime(nowTime.year, nowTime.month, nowTime.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取昨天的开始时间
  static int getBeginDayOfYesterday(){
    var nowTime = DateTime.now();
    var yesterday = nowTime.add(new Duration(days: -1));
    var day = new DateTime(yesterday.year, yesterday.month, yesterday.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取昨天的结束时间
  static int getEndDayOfYesterDay(){
    var nowTime = DateTime.now();
    var yesterday = nowTime.add(new Duration(days: -1));
    var day = new DateTime(yesterday.year, yesterday.month, yesterday.day, 23,  59, 59);
    return day.millisecondsSinceEpoch;
  }

  ///获取本周的开始时间
  static int getBeginDayOfWeek(){
    var nowTime = DateTime.now();
    var weekday=nowTime.weekday;
    var yesterday = nowTime.add(new Duration(days: -(weekday-1)));
    var day = new DateTime(yesterday.year, yesterday.month, yesterday.day, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取本月的开始时间
  static int getBeginDayOfMonth(){
    var nowTime = DateTime.now();
    var day = new DateTime(nowTime.year, nowTime.month, 1, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

  ///获取本年的开始时间
  static int getBeginDayOfYear(){
    var nowTime = DateTime.now();
    var day = new DateTime(nowTime.year, 1, 1, 0,  0, 0);
    return day.millisecondsSinceEpoch;
  }

}

相关文章

网友评论

      本文标题:flutter时间工具类,获取具体某时间的13位时间戳

      本文链接:https://www.haomeiwen.com/subject/uepcyktx.html