Flutter时间日期转换工具类

我们知道Flutter系统自带的时间方法类DateTime其中有两个方法

  //这里传入毫秒
  external DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
      {bool isUtc = false});
  //这里传入微秒(1毫秒=1000微秒)
        external DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch,
      {bool isUtc = false});

仅有这两个方法是达不到我们想要的结果的,需要自己进行封装

话不多说直接上工具类

import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
/*
 * 关于时间工具
 */
class DateUtils {
  // 工厂模式
  factory DateUtils() => _getInstance();

  static DateUtils get instance => _getInstance();
  static DateUtils _instance;

  DateUtils._internal() {
    // 初始化
  }

  static DateUtils _getInstance() {
    if (_instance == null) {
      _instance = new DateUtils._internal();
    }
    return _instance;
  }

  ///将时间日期格式转化为时间戳
  ///2018年12月11日
  ///2019-12-11
  ///2018年11月15 11:14分89
  ///结果是毫秒
  int getTimeStap({formartData: String}) {
    var result = formartData.substring(0, 4) + "-" + formartData.substring(5, 7) + "-" + formartData.substring(8, 10);
    if (formartData.toString().length>=13&&formartData.substring(10, 13) != null) {
      result += "" + formartData.substring(10, 13);
    }
    if (formartData.toString().length>=17&&formartData.toString().substring(14, 16) != null) {
      result += ":" + formartData.substring(14, 16);
    }
    if (formartData.toString().length>=19&&formartData.substring(17, 19) != null) {
      result += ":" + formartData.substring(17, 19);
    }
    var dataTime = DateTime.parse(result);
    print(dataTime.millisecondsSinceEpoch);
    return dataTime.millisecondsSinceEpoch;
  }

  ///格式化时间戳
  ///timeSamp:毫秒值
  ///format:"yyyy年MM月dd hh:mm:ss"  "yyy?MM?dd  hh?MM?dd" "yyyy:MM:dd"......
  ///结果: 2019?08?04  02?08?02
  getFormartData({timeSamp: int, format: String}) {
    var dataFormart = new DateFormat(format);
    var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
    String formartResult = dataFormart.format(dateTime);
    return formartResult;
  }

  ///1.获取从某一天开始到某一天结束的所有的中间日期,例如输入 startTime:2019:07:31  endTime:2019:08:31  就会返回所有的中间天数。
  ///startTime和endTime格式如下都可以
  ///使用:    List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  ///结果:[2019年07月11, 2019年07月12, 2019年07月13, 2019年07月14, 2019年07月15, 2019年07月16, 2019年07月17, 2019年07月18, 2019年07月19, 2019年07月20, 2019年07月21, 2019年07月22, 2019年07月23, 2019年07月24, 2019年07月25, 2019年07月26, 2019年07月27, 2019年07月28, 2019年07月29, 2019年07月30, 2019年07月31, 2019年08月01, 2019年08月02, 2019年08月03, 2019年08月04, 2019年08月05, 2019年08月06, 2019年08月07, 2019年08月08, 2019年08月09, 2019年08月10, 2019年08月11, 2019年08月12, 2019年08月13, 2019年08月14, 2019年08月15, 2019年08月16, 2019年08月17, 2019年08月18, 2019年08月19, 2019年08月20, 2019年08月21, 2019年08月22, 2019年08月23, 2019年08月24, 2019年08月25, 2019年08月26, 2019年08月27, 2019年08月28, 2019年08月29]
  List<String> getTimeBettwenStartTimeAndEnd(
      {startTime: String, endTime: String, format: String}) {
    var mDataList = List<String>();
    //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
    int allTimeEnd = 0;
    //记录当前到个数(相当于天数)
    int currentFlag = 0;
    DateTime startData = DateTime.parse(startTime);
    DateTime endData = DateTime.parse(endTime);
    var mothFormatFlag = new DateFormat(format);
    while (endData.millisecondsSinceEpoch > allTimeEnd) {
      allTimeEnd =
          startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000;
      var dateTime = new DateTime.fromMillisecondsSinceEpoch(
          startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
      String nowMoth = mothFormatFlag.format(dateTime);
      mDataList.add(nowMoth);
      currentFlag++;
    }
    return mDataList;
  }

  ///传入starTime格式 2012-02-27 13:27:00 或者 "2012-02-27等....
  ///dayNumber:从startTime往后面多少天你需要输出
  ///formart:获取到的日期格式。"yyyy年MM月dd" "yyyy-MM-dd" "yyyy年" "yyyy年MM月" "yyyy年\nMM月dd"  等等
  ///使用:DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  ///结果:[2019年07月11, 2019年07月12, 2019年07月13, 2019年07月14, 2019年07月15, 2019年07月16, 2019年07月17, 2019年07月18, 2019年07月19, 2019年07月20, 2019年07月21]
  List<String> getTimeStartTimeAndEnd(
      {startTime: String, dayNumber: int, format: String}) {
    var mDataList = List<String>();
    //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
    int allTimeEnd = 0;
    //记录当前到个数(相当于天数)
    int currentFlag = 0;
    DateTime startData = DateTime.parse(startTime);
    var mothFormatFlag = new DateFormat(format);
    while (dayNumber >= currentFlag) {
      var dateTime = new DateTime.fromMillisecondsSinceEpoch(
          startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
      String nowMoth = mothFormatFlag.format(dateTime);
      mDataList.add(nowMoth);
      currentFlag++;
    }
    return mDataList;
  }
  ///startTime:输入其实时间的时间戳也可以。
  ///dayNumber:时间段
  ///输入时间格式
  List<TimeData> getTimeStartTimeAndEndTime({startTime: int, dayNumber: int, format: String}) {
    var mDataList = List<TimeData>();
    //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
    int allTimeEnd = 0;
    //记录当前到个数(相当于天数)
    int currentFlag = 0;
    var mothFormatFlag = new DateFormat(format);
    while (dayNumber >= currentFlag) {
      TimeData timeData=new TimeData();
      var dateTime = new DateTime.fromMillisecondsSinceEpoch(startTime + currentFlag * 24 * 60 * 60 * 1000);
      String nowMoth = mothFormatFlag.format(dateTime);
      timeData.dataTime=nowMoth;
      timeData.week=dateTime.weekday;
      mDataList.add(timeData);
      currentFlag++;
    }
    return mDataList;
  }


  ///获取某一个月的最后一天。
  ///我们能提供和知道的条件有:(当天的时间,)
  ///timeSamp:时间戳 单位(毫秒)
  ///format:想要的格式  "yyyy年MM月dd hh:mm:ss"  "yyy?MM?dd  hh?MM?dd" "yyyy:MM:dd"
  getEndMoth({timeSamp: int, format: String}) {
    var dataFormart = new DateFormat(format);
    var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
    var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
    int nextTimeSamp =
        dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
    //取得了下一个月1号码时间戳
    var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
    String formartResult = dataFormart.format(dateTimeeee);
    return formartResult;
  }

  ///获取某一个月的最后一天。
  ///我们能提供和知道的条件有:(当天的时间,)
  ///timeSamp:传入的是时间格式
  ///format:想要的格式  "yyyy年MM月dd hh:mm:ss"  "yyy?MM?dd  hh?MM?dd" "yyyy:MM:dd"
  getEndMothFor({mothFormart: String, format: String}) {
    DateTime startData = DateTime.parse(mothFormart);
    var dataFormart = new DateFormat(format);
    var dateTime = new DateTime.fromMillisecondsSinceEpoch(
        startData.millisecondsSinceEpoch);
    var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
    int nextTimeSamp =
        dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
    //取得了下一个月1号码时间戳
    var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
    String formartResult = dataFormart.format(dateTimeeee);
    return formartResult;
  }
  // 获取星期
  static String getWeek(DateTime date){
    var week = date.weekday;
    String w = '';
    switch (week.toString()) {
      case '1':
        w = '一';
        break;
      case '2':
        w = '二';
        break;
      case '3':
        w = '三';
        break;
      case '4':
        w = '四';
        break;
      case '5':
        w = '五';
        break;
      case '6':
        w = '六';
        break;
      case '7':
        w = '日';
        break;
    }
    return '周' + w.toString();
  }
}
class TimeData{
  String dataTime;
  int week;
}

下面是使用:

//    ///使用:
//    //获取两个时间段之间的所有的日期
//    List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
//    print(mdata.toString());
//    //获取从那一天开始的之后多少天之内的所有日期
//    List<String>mdates=DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
//    print(mdates);
//    //通过时间戳来获取自己想要格式的日期。
//    DateUtils.instance.getFormartData(timeSamp:new DateTime.now().millisecondsSinceEpoch+3 * 24 * 60 * 60 * 1000,format:"yyy?MM?dd  hh?MM?ss");
//     //获取某一个月的最后一天。传入的是时间戳(微妙)
//    String datad=DateUtils.instance.getEndMoth(timeSamp:new DateTime.now().millisecondsSinceEpoch-180 * 24 * 60 * 60 * 1000,format:"yyyy年MM月dd hh:MM:ss");
//    print(datad);
//     //获取某一个月的最后一天。传入的是日期(微妙)
//    String mothData=DateUtils.instance.getEndMothFor(mothFormart:"2019-02-11",format:"yyy年MM月dd hh?MM⏲️:ss");
//    print(mothData);
//  结果:
//flutter: [2019年07月11, 2019年07月12, 2019年07月13, 2019年07月14, 2019年07月15, 2019年07月16, 2019年07月17, 2019年07月18, 2019年07月19, 2019年07月20, 2019年07月21, 2019年07月22, 2019年07月23, 2019年07月24, 2019年07月25, 2019年07月26, 2019年07月27, 2019年07月28, 2019年07月29, 2019年07月30, 2019年07月31, 2019年08月01, 2019年08月02, 2019年08月03, 2019年08月04, 2019年08月05, 2019年08月06, 2019年08月07, 2019年08月08, 2019年08月09, 2019年08月10, 2019年08月11, 2019年08月12, 2019年08月13, 2019年08月14, 2019年08月15, 2019年08月16, 2019年08月17, 2019年08月18, 2019年08月19, 2019年08月20, 2019年08月21, 2019年08月22, 2019年08月23, 2019年08月24, 2019年08月25, 2019年08月26, 2019年08月27, 2019年08月28, 2019年08月29]
//flutter: [2019年07月11, 2019年07月12, 2019年07月13, 2019年07月14, 2019年07月15, 2019年07月16, 2019年07月17, 2019年07月18, 2019年07月19, 2019年07月20, 2019年07月21]
//flutter: 2019?08?04  03?08?16
//flutter: 年=2019
//flutter: 月=2
//flutter: 2019年02月28 12:02:00
//flutter: 年=2019
//flutter: 月=2
//flutter: 2019年02月28 12?02⏲️:00 

用的比较多的就是前2个方法了

DateUtils.instance.getFormartData(timeSamp:vorderList[index].iOrderTime,format:“HH:mm MM/dd”),//输出 10:15 09/23
可以自定义你想要的各种格式
这里要注意的是 小时分钟是用的HH:mm
H大写代表24小时制
如果改成hh:mm 则是12小时制

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值