123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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
- String getFormartData({timeSamp: int, format: String}) {
- var dataFormart = new DateFormat(format);
- var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
- String formartResult = dataFormart.format(dateTime);
- return formartResult;
- }
- String getFormattedDateTimeStr({DateTime datetime, String format}) {
- var dateFormat = new DateFormat(format);
- return dateFormat.format(datetime);
- }
- ///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;
- }
- // getSurplus(int date, {int min}) {
- // }
- ///获取某一个月的最后一天。
- ///我们能提供和知道的条件有:(当天的时间,)
- ///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();
- }
- //获取几天几秒前
- String getDaysformat(DateTime date) {
- final num ONE_MINUTE = 60000;
- final num ONE_HOUR = 3600000;
- final num ONE_DAY = 86400000;
- final num ONE_WEEK = 604800000;
- final String ONE_SECOND_AGO = "秒前";
- final String ONE_MINUTE_AGO = "分钟前";
- final String ONE_HOUR_AGO = "小时前";
- final String ONE_DAY_AGO = "天前";
- final String ONE_MONTH_AGO = "月前";
- final String ONE_YEAR_AGO = "年前";
- num toSeconds(num date) {
- return date / 1000;
- }
- num toMinutes(num date) {
- return toSeconds(date) / 60;
- }
- num toHours(num date) {
- return toMinutes(date) / 60;
- }
- num toDays(num date) {
- return toHours(date) / 24;
- }
- num toMonths(num date) {
- return toDays(date) / 30;
- }
- num toYears(num date) {
- return toMonths(date) / 365;
- }
- num delta =
- DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
- if (delta < 1 * ONE_MINUTE) {
- num seconds = toSeconds(delta);
- return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
- }
- if (delta < 45 * ONE_MINUTE) {
- num minutes = toMinutes(delta);
- return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
- }
- if (delta < 24 * ONE_HOUR) {
- num hours = toHours(delta);
- return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
- }
- if (delta < 48 * ONE_HOUR) {
- return "昨天";
- }
- if (delta < 30 * ONE_DAY) {
- num days = toDays(delta);
- return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO;
- }
- if (delta < 12 * 4 * ONE_WEEK) {
- num months = toMonths(delta);
- return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO;
- } else {
- num years = toYears(delta);
- return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO;
- }
- }
- }
- 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
|