Flutter获取上个月,近半年,近一年的具体日期

958 阅读2分钟
import 'dart:ffi';

class DateUtil {

  ///获取上个月的日期
  static DateTime getLastMonth() {
    DateTime startTime;
    DateTime now = DateTime.now();
    var year = now.year;
    var month = now.month;
    var day = now.day;
    //上个月份天数
    var lastYearMonthDay = getMonthDay(year, month - 1);
    if(lastYearMonthDay < day) {
      startTime = DateTime(year, month -1, lastYearMonthDay);
    } else {
      startTime = DateTime(year, month -1, day);
    }
    return startTime;
  }

  /// 获取近半年的日期
  static DateTime getLastHalfYear() {
    DateTime startTime;
    DateTime now = DateTime.now();
    var year = now.year;
    var month = now.month;
    var day = now.day;
    //当前月份天数
    var nowMonthDay = getMonthDay(year, month);

    if(month -6 <= 0) {
      var lastMonthDay = getMonthDay(year - 1, 12 - (6 - month));
      ///上个月的总天数少于当前日的情况
      if(lastMonthDay < day) {
        startTime = DateTime(year - 1, 12 - (6 - month), lastMonthDay);
      } else {
        startTime = DateTime(year - 1, 12 - (6 - month), day);
      }
    } else {
      var lastMonthDay = getMonthDay(year, month - 6);
      if(lastMonthDay < day) {
      ///二月份的特殊情况
        if(day < nowMonthDay) {
          startTime = DateTime(year, month - 6, lastMonthDay - (nowMonthDay - day));
        } else {
          startTime = DateTime(year, month - 6, lastMonthDay);
        }
      } else {
        startTime = DateTime(year, month - 6, day);
      }
    }
    return startTime;
  }

  ///获取近一年的日期
  static DateTime getLastYearDate() {
    DateTime startTime;
    DateTime now = DateTime.now();
    var year = now.year;
    var month = now.month;
    var day = now.day;
    //去年月份天数
    var lastYearMonthDay = getMonthDay(year - 1, month);
    if(lastYearMonthDay < day) {
      startTime = DateTime(year -1, month, lastYearMonthDay);
    } else {
      startTime = DateTime(year -1, month, day);
    }
    return startTime;
  }
///获取某年某月的总天数
  static int getMonthDay(int year, int month) {
    var nowMonthDay = 30;
    if(month == 12) {
      nowMonthDay = DateTime(year + 1, month, 1).difference(DateTime(year, month, 1)).inDays;
    } else {
      nowMonthDay = DateTime(year, month + 1, 1).difference(DateTime(year, month, 1)).inDays;
    }
    return nowMonthDay;
  }
}

上面注释还是很详细的,就是计算本月的天数时,是取得下个月的一号和当前月的一号相差的天数。 如果要想将dateTime转成时间戳,可以直接使用DateTime所提供的方法:

/// The number of milliseconds since
/// the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
///
/// This value is independent of the time zone.
///
/// This value is at most
/// 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch.
/// In other words: `millisecondsSinceEpoch.abs() <= 8640000000000000`.
external int get millisecondsSinceEpoch;
```这里获取的即为毫秒值
DateTime转换日期的格式可以直接使用```
import 'package:intl/intl.dart'包下的方法:
DateFormat("yyyy.MM.dd").format(DateTime());

下面这个方法是时间戳转DateTime的方法:

/// Constructs a new [DateTime] instance
/// with the given [millisecondsSinceEpoch].
///
/// If [isUtc] is false then the date is in the local time zone.
///
/// The constructed [DateTime] represents
/// 1970-01-01T00:00:00Z + [millisecondsSinceEpoch] ms in the given
/// time zone (local or UTC).
/// ```dart
/// final newYearsDay =
///     DateTime.fromMillisecondsSinceEpoch(1640979000000, isUtc:true);
/// print(newYearsDay); // 2022-01-01 10:00:00.000Z
/// ```
external DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
    {bool isUtc = false});

isUtc = false是指本地时区,OK,Over!