time_format.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import 'package:intl/intl.dart';
  2. /*
  3. * 关于时间工具
  4. */
  5. class DateUtils {
  6. // 工厂模式
  7. factory DateUtils() => _getInstance();
  8. static DateUtils get instance => _getInstance();
  9. static DateUtils _instance;
  10. DateUtils._internal() {
  11. // 初始化
  12. }
  13. static DateUtils _getInstance() {
  14. if (_instance == null) {
  15. _instance = new DateUtils._internal();
  16. }
  17. return _instance;
  18. }
  19. ///将时间日期格式转化为时间戳
  20. ///2018年12月11日
  21. ///2019-12-11
  22. ///2018年11月15 11:14分89
  23. ///结果是毫秒
  24. int getTimeStap({formartData: String}) {
  25. var result = formartData.substring(0, 4) +
  26. "-" +
  27. formartData.substring(5, 7) +
  28. "-" +
  29. formartData.substring(8, 10);
  30. if (formartData.toString().length >= 13 &&
  31. formartData.substring(10, 13) != null) {
  32. result += "" + formartData.substring(10, 13);
  33. }
  34. if (formartData.toString().length >= 17 &&
  35. formartData.toString().substring(14, 16) != null) {
  36. result += ":" + formartData.substring(14, 16);
  37. }
  38. if (formartData.toString().length >= 19 &&
  39. formartData.substring(17, 19) != null) {
  40. result += ":" + formartData.substring(17, 19);
  41. }
  42. var dataTime = DateTime.parse(result);
  43. print(dataTime.millisecondsSinceEpoch);
  44. return dataTime.millisecondsSinceEpoch;
  45. }
  46. ///格式化时间戳
  47. ///timeSamp:毫秒值
  48. ///format:"yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"......
  49. ///结果: 2019?08?04 02?08?02
  50. String getFormartData({timeSamp: int, format: String}) {
  51. var dataFormart = new DateFormat(format);
  52. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  53. String formartResult = dataFormart.format(dateTime);
  54. return formartResult;
  55. }
  56. String getFormattedDateTimeStr({DateTime datetime, String format}) {
  57. var dateFormat = new DateFormat(format);
  58. return dateFormat.format(datetime);
  59. }
  60. ///1.获取从某一天开始到某一天结束的所有的中间日期,例如输入 startTime:2019:07:31 endTime:2019:08:31 就会返回所有的中间天数。
  61. ///startTime和endTime格式如下都可以
  62. ///使用: List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  63. ///结果:[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]
  64. List<String> getTimeBettwenStartTimeAndEnd(
  65. {startTime: String, endTime: String, format: String}) {
  66. var mDataList = List<String>();
  67. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  68. int allTimeEnd = 0;
  69. //记录当前到个数(相当于天数)
  70. int currentFlag = 0;
  71. DateTime startData = DateTime.parse(startTime);
  72. DateTime endData = DateTime.parse(endTime);
  73. var mothFormatFlag = new DateFormat(format);
  74. while (endData.millisecondsSinceEpoch > allTimeEnd) {
  75. allTimeEnd =
  76. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000;
  77. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  78. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  79. String nowMoth = mothFormatFlag.format(dateTime);
  80. mDataList.add(nowMoth);
  81. currentFlag++;
  82. }
  83. return mDataList;
  84. }
  85. ///传入starTime格式 2012-02-27 13:27:00 或者 "2012-02-27等....
  86. ///dayNumber:从startTime往后面多少天你需要输出
  87. ///formart:获取到的日期格式。"yyyy年MM月dd" "yyyy-MM-dd" "yyyy年" "yyyy年MM月" "yyyy年\nMM月dd" 等等
  88. ///使用:DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  89. ///结果:[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]
  90. List<String> getTimeStartTimeAndEnd(
  91. {startTime: String, dayNumber: int, format: String}) {
  92. var mDataList = List<String>();
  93. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  94. int allTimeEnd = 0;
  95. //记录当前到个数(相当于天数)
  96. int currentFlag = 0;
  97. DateTime startData = DateTime.parse(startTime);
  98. var mothFormatFlag = new DateFormat(format);
  99. while (dayNumber >= currentFlag) {
  100. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  101. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  102. String nowMoth = mothFormatFlag.format(dateTime);
  103. mDataList.add(nowMoth);
  104. currentFlag++;
  105. }
  106. return mDataList;
  107. }
  108. ///startTime:输入其实时间的时间戳也可以。
  109. ///dayNumber:时间段
  110. ///输入时间格式
  111. List<TimeData> getTimeStartTimeAndEndTime(
  112. {startTime: int, dayNumber: int, format: String}) {
  113. var mDataList = List<TimeData>();
  114. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  115. int allTimeEnd = 0;
  116. //记录当前到个数(相当于天数)
  117. int currentFlag = 0;
  118. var mothFormatFlag = new DateFormat(format);
  119. while (dayNumber >= currentFlag) {
  120. TimeData timeData = new TimeData();
  121. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  122. startTime + currentFlag * 24 * 60 * 60 * 1000);
  123. String nowMoth = mothFormatFlag.format(dateTime);
  124. timeData.dataTime = nowMoth;
  125. timeData.week = dateTime.weekday;
  126. mDataList.add(timeData);
  127. currentFlag++;
  128. }
  129. return mDataList;
  130. }
  131. // getSurplus(int date, {int min}) {
  132. // }
  133. ///获取某一个月的最后一天。
  134. ///我们能提供和知道的条件有:(当天的时间,)
  135. ///timeSamp:时间戳 单位(毫秒)
  136. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"
  137. getEndMoth({timeSamp: int, format: String}) {
  138. var dataFormart = new DateFormat(format);
  139. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  140. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  141. int nextTimeSamp =
  142. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  143. //取得了下一个月1号码时间戳
  144. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  145. String formartResult = dataFormart.format(dateTimeeee);
  146. return formartResult;
  147. }
  148. ///获取某一个月的最后一天。
  149. ///我们能提供和知道的条件有:(当天的时间,)
  150. ///timeSamp:传入的是时间格式
  151. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"
  152. getEndMothFor({mothFormart: String, format: String}) {
  153. DateTime startData = DateTime.parse(mothFormart);
  154. var dataFormart = new DateFormat(format);
  155. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  156. startData.millisecondsSinceEpoch);
  157. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  158. int nextTimeSamp =
  159. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  160. //取得了下一个月1号码时间戳
  161. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  162. String formartResult = dataFormart.format(dateTimeeee);
  163. return formartResult;
  164. }
  165. // 获取星期
  166. static String getWeek(DateTime date) {
  167. var week = date.weekday;
  168. String w = '';
  169. switch (week.toString()) {
  170. case '1':
  171. w = '一';
  172. break;
  173. case '2':
  174. w = '二';
  175. break;
  176. case '3':
  177. w = '三';
  178. break;
  179. case '4':
  180. w = '四';
  181. break;
  182. case '5':
  183. w = '五';
  184. break;
  185. case '6':
  186. w = '六';
  187. break;
  188. case '7':
  189. w = '日';
  190. break;
  191. }
  192. return '周' + w.toString();
  193. }
  194. //获取几天几秒前
  195. String getDaysformat(DateTime date) {
  196. final num ONE_MINUTE = 60000;
  197. final num ONE_HOUR = 3600000;
  198. final num ONE_DAY = 86400000;
  199. final num ONE_WEEK = 604800000;
  200. final String ONE_SECOND_AGO = "秒前";
  201. final String ONE_MINUTE_AGO = "分钟前";
  202. final String ONE_HOUR_AGO = "小时前";
  203. final String ONE_DAY_AGO = "天前";
  204. final String ONE_MONTH_AGO = "月前";
  205. final String ONE_YEAR_AGO = "年前";
  206. num toSeconds(num date) {
  207. return date / 1000;
  208. }
  209. num toMinutes(num date) {
  210. return toSeconds(date) / 60;
  211. }
  212. num toHours(num date) {
  213. return toMinutes(date) / 60;
  214. }
  215. num toDays(num date) {
  216. return toHours(date) / 24;
  217. }
  218. num toMonths(num date) {
  219. return toDays(date) / 30;
  220. }
  221. num toYears(num date) {
  222. return toMonths(date) / 365;
  223. }
  224. num delta =
  225. DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
  226. if (delta < 1 * ONE_MINUTE) {
  227. num seconds = toSeconds(delta);
  228. return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
  229. }
  230. if (delta < 45 * ONE_MINUTE) {
  231. num minutes = toMinutes(delta);
  232. return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
  233. }
  234. if (delta < 24 * ONE_HOUR) {
  235. num hours = toHours(delta);
  236. return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
  237. }
  238. if (delta < 48 * ONE_HOUR) {
  239. return "昨天";
  240. }
  241. if (delta < 30 * ONE_DAY) {
  242. num days = toDays(delta);
  243. return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO;
  244. }
  245. if (delta < 12 * 4 * ONE_WEEK) {
  246. num months = toMonths(delta);
  247. return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO;
  248. } else {
  249. num years = toYears(delta);
  250. return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO;
  251. }
  252. }
  253. }
  254. class TimeData {
  255. String dataTime;
  256. int week;
  257. }
  258. // ///使用:
  259. // //获取两个时间段之间的所有的日期
  260. // List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  261. // print(mdata.toString());
  262. // //获取从那一天开始的之后多少天之内的所有日期
  263. // List<String>mdates=DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  264. // print(mdates);
  265. // //通过时间戳来获取自己想要格式的日期。
  266. // DateUtils.instance.getFormartData(timeSamp:new DateTime.now().millisecondsSinceEpoch+3 * 24 * 60 * 60 * 1000,format:"yyy?MM?dd hh?MM?ss");
  267. // //获取某一个月的最后一天。传入的是时间戳(微妙)
  268. // String datad=DateUtils.instance.getEndMoth(timeSamp:new DateTime.now().millisecondsSinceEpoch-180 * 24 * 60 * 60 * 1000,format:"yyyy年MM月dd hh:MM:ss");
  269. // print(datad);
  270. // //获取某一个月的最后一天。传入的是日期(微妙)
  271. // String mothData=DateUtils.instance.getEndMothFor(mothFormart:"2019-02-11",format:"yyy年MM月dd hh?MM⏲️:ss");
  272. // print(mothData);
  273. // 结果:
  274. //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]
  275. //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]
  276. //flutter: 2019?08?04 03?08?16
  277. //flutter: 年=2019
  278. //flutter: 月=2
  279. //flutter: 2019年02月28 12:02:00
  280. //flutter: 年=2019
  281. //flutter: 月=2
  282. //flutter: 2019年02月28 12?02⏲️:00