flutter 日期格式工具

学习笔记 yekong 61℃
计算两个日期相差多少年
计算两个日期相差多少月
计算两个日期相差多少天
计算两个日期相差多少分钟
计算两个日期相差多少秒
计算两个日期相差多少毫秒
获取当天(不足两位,拼0处理)
获取昨天
获取前天
获取明天
获取后天
获取本周开始
获取本周结束
获取上周开始
获取上周结束
获取本月第一天
获取本月最后一天
获取周几
获取一个月有多少天

class DateUtils {
  /**
   * 计算两个日期相差多少年
   */
  static int daysBetweenYear(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v ~/ 86400000 * 30 * 12;
  }

  /**
   * 计算两个日期相差多少月
   */
  static int daysBetweenMonth(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v ~/ 86400000 * 30;
  }

  /**
   * 计算两个日期相差多少天
   */
  static int daysBetweenDay(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v ~/ 86400000;
  }

  /**
   * 计算两个日期相差多少分钟
   */
  static int daysBetweenMin(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v ~/ 60000;
  }

  /**
   * 计算两个日期相差多少秒
   */
  static int daysBetweenSecond(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v ~/ 1000;
  }

  /**
   * 计算两个日期相差多少毫秒
   */
  static int daysBetweenMillSecond(DateTime a, DateTime b) {
    int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
    return v;
  }

  /**
   * 获取当天(不足两位,拼0处理)
   * space 需要拼接日期的字段
   */
  static String getYYYYMMDD(DateTime dateTime, String space) {
    String year = dateTime.year.toString();
    String month = dateTime.month.toString().length == 1
        ? "0${dateTime.month}"
        : dateTime.month.toString();
    String day = dateTime.day.toString().length == 1
        ? "0${dateTime.day}"
        : dateTime.day.toString();
    return "${year}${space}${month}${space}${day}";
  }

  /**
   * 获取当天(不足两位,拼0处理)
   * space 需要拼接日期的字段
   */
  static String getYYYYMMDDHHMMSS(DateTime dateTime, String space) {
    String year = dateTime.year.toString();
    String month = dateTime.month.toString().length == 1
        ? "0${dateTime.month}"
        : dateTime.month.toString();
    String day = dateTime.day.toString().length == 1
        ? "0${dateTime.day}"
        : dateTime.day.toString();
    String hour = dateTime.hour.toString().length == 1
        ? "0${dateTime.hour}"
        : dateTime.hour.toString();
    String minute = dateTime.minute.toString().length == 1
        ? "0${dateTime.minute}"
        : dateTime.minute.toString();
    String second = dateTime.second.toString().length == 1
        ? "0${dateTime.second}"
        : dateTime.second.toString();
    return "${year}${space}${month}${space}${day} ${hour}${space}${minute}${space}${second}";
  }

  /**
   * 获取昨天
   */
  static String getYesterDayYYYYMMDD(DateTime dateTime) {
    DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch - (24 * 60 * 60 * 1000));
    return getYYYYMMDD(yesterDay, "-");
  }

  /**
   * 获取前天
   */
  static String getDayBeforeYesterdayDayYYYYMMDD(DateTime dateTime) {
    DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch - (2 * 24 * 60 * 60 * 1000));
    return getYYYYMMDD(yesterDay, "-");
  }

  /**
   * 获取明天
   */
  static String getTomorrowDayYYYYMMDD(DateTime dateTime) {
    DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch + (24 * 60 * 60 * 1000));
    return getYYYYMMDD(yesterDay, "-");
  }

  /**
   * 获取后天
   */
  static String getTomorrowAcquiredYYYYMMDD(DateTime dateTime) {
    DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch + (2 * 24 * 60 * 60 * 1000));
    return getYYYYMMDD(yesterDay, "-");
  }

  /**
   * 获取本周开始
   */
  static String getWeekFirstDayYYYYMMDD(DateTime dateTime) {
    int current = dateTime.weekday;
    DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch -
            (24 * 60 * 60 * 1000 * (current - 1)));
    return getYYYYMMDD(firstDay, "-");
  }

  /**
   * 获取本周结束
   */
  static String getWeekLastDayYYYYMMDD(DateTime dateTime) {
    int current = dateTime.weekday;
    DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch +
            (24 * 60 * 60 * 1000 * (7 - current)));
    return getYYYYMMDD(lastDay, "-");
  }

  /**
   * 获取上周开始
   */
  static String getLastWeekFirstDayYYYYMMDD(DateTime dateTime) {
    int current = dateTime.weekday;
    DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch -
            (24 * 60 * 60 * 1000 * (current - 1)));
    DateTime day = new DateTime.fromMillisecondsSinceEpoch(
        firstDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
    return getYYYYMMDD(day, "-");
  }

  /**
   * 获取上周结束
   */
  static String getLastWeekLastDayYYYYMMDD(DateTime dateTime) {
    int current = dateTime.weekday;
    DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
        dateTime.millisecondsSinceEpoch +
            (24 * 60 * 60 * 1000 * (7 - current)));
    DateTime day = new DateTime.fromMillisecondsSinceEpoch(
        lastDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
    return getYYYYMMDD(day, "-");
  }

  /**
   * 获取本月第一天
   */
  static String getMonthFirstDayYYYYMMDD(DateTime dateTime, String space) {
    String year = "${DateTime.now().year}";
    String month = "${DateTime.now().month}".length == 1
        ? "0${DateTime.now().month}"
        : "${DateTime.now().month}";
    return "${year}${space}${month}${space}01";
  }

  /**
   * 获取本月最后一天
   */
  static String getMonthLastDayYYYYMMDD(DateTime dateTime, String space) {
    String year = "${DateTime.now().year}";
    String month = "${DateTime.now().month}".length == 1
        ? "0${DateTime.now().month}"
        : "${DateTime.now().month}";
    int d = getDayCounts(DateTime.now().month);
    return "${year}${space}${month}${space}${d}";
  }

  /**
   * 获取周几
   */
  static String getWeekday(DateTime dateTime) {
    if (dateTime == null) return null;
    String weekday;
    switch (dateTime.weekday) {
      case 1:
        weekday = '星期一';
        break;
      case 2:
        weekday = '星期二';
        break;
      case 3:
        weekday = '星期三';
        break;
      case 4:
        weekday = '星期四';
        break;
      case 5:
        weekday = '星期五';
        break;
      case 6:
        weekday = '星期六';
        break;
      case 7:
        weekday = '星期日';
        break;
      default:
        break;
    }
    return weekday;
  }

  /**
   * 获取一个月有多少天
   */
  static int getDayCounts(int month) {
    int year = DateTime.now().year;

    int end = 0;
    if (month == 1) {
      end = 31;
    } else if (month == 2) {
      if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
        end = 29;
      } else {
        end = 28;
      }
    } else if (month == 3) {
      end = 31;
    } else if (month == 4) {
      end = 30;
    } else if (month == 5) {
      end = 31;
    } else if (month == 6) {
      end = 30;
    } else if (month == 7) {
      end = 31;
    } else if (month == 8) {
      end = 31;
    } else if (month == 9) {
      end = 30;
    } else if (month == 10) {
      end = 31;
    } else if (month == 11) {
      end = 30;
    } else if (month == 12) {
      end = 31;
    }

    return end;
  }
}
喜欢 (2)