time_format.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:intl/intl.dart';
  3. /*
  4. * 关于时间工具
  5. */
  6. class DateUtils {
  7. // 工厂模式
  8. factory DateUtils() => _getInstance();
  9. static DateUtils get instance => _getInstance();
  10. static DateUtils _instance;
  11. DateUtils._internal() {
  12. // 初始化
  13. }
  14. static DateUtils _getInstance() {
  15. if (_instance == null) {
  16. _instance = new DateUtils._internal();
  17. }
  18. return _instance;
  19. }
  20. ///将时间日期格式转化为时间戳
  21. ///2018年12月11日
  22. ///2019-12-11
  23. ///2018年11月15 11:14分89
  24. ///结果是毫秒
  25. int getTimeStap({formartData: String}) {
  26. var result = formartData.substring(0, 4) + "-" + formartData.substring(5, 7) + "-" + formartData.substring(8, 10);
  27. if (formartData.toString().length>=13&&formartData.substring(10, 13) != null) {
  28. result += "" + formartData.substring(10, 13);
  29. }
  30. if (formartData.toString().length>=17&&formartData.toString().substring(14, 16) != null) {
  31. result += ":" + formartData.substring(14, 16);
  32. }
  33. if (formartData.toString().length>=19&&formartData.substring(17, 19) != null) {
  34. result += ":" + formartData.substring(17, 19);
  35. }
  36. var dataTime = DateTime.parse(result);
  37. print(dataTime.millisecondsSinceEpoch);
  38. return dataTime.millisecondsSinceEpoch;
  39. }
  40. ///格式化时间戳
  41. ///timeSamp:毫秒值
  42. ///format:"yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"......
  43. ///结果: 2019?08?04 02?08?02
  44. getFormartData({timeSamp: int, format: String}) {
  45. var dataFormart = new DateFormat(format);
  46. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  47. String formartResult = dataFormart.format(dateTime);
  48. return formartResult;
  49. }
  50. ///1.获取从某一天开始到某一天结束的所有的中间日期,例如输入 startTime:2019:07:31 endTime:2019:08:31 就会返回所有的中间天数。
  51. ///startTime和endTime格式如下都可以
  52. ///使用: List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  53. ///结果:[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]
  54. List<String> getTimeBettwenStartTimeAndEnd(
  55. {startTime: String, endTime: String, format: String}) {
  56. var mDataList = List<String>();
  57. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  58. int allTimeEnd = 0;
  59. //记录当前到个数(相当于天数)
  60. int currentFlag = 0;
  61. DateTime startData = DateTime.parse(startTime);
  62. DateTime endData = DateTime.parse(endTime);
  63. var mothFormatFlag = new DateFormat(format);
  64. while (endData.millisecondsSinceEpoch > allTimeEnd) {
  65. allTimeEnd =
  66. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000;
  67. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  68. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  69. String nowMoth = mothFormatFlag.format(dateTime);
  70. mDataList.add(nowMoth);
  71. currentFlag++;
  72. }
  73. return mDataList;
  74. }
  75. ///传入starTime格式 2012-02-27 13:27:00 或者 "2012-02-27等....
  76. ///dayNumber:从startTime往后面多少天你需要输出
  77. ///formart:获取到的日期格式。"yyyy年MM月dd" "yyyy-MM-dd" "yyyy年" "yyyy年MM月" "yyyy年\nMM月dd" 等等
  78. ///使用:DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  79. ///结果:[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]
  80. List<String> getTimeStartTimeAndEnd(
  81. {startTime: String, dayNumber: int, format: String}) {
  82. var mDataList = List<String>();
  83. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  84. int allTimeEnd = 0;
  85. //记录当前到个数(相当于天数)
  86. int currentFlag = 0;
  87. DateTime startData = DateTime.parse(startTime);
  88. var mothFormatFlag = new DateFormat(format);
  89. while (dayNumber >= currentFlag) {
  90. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  91. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  92. String nowMoth = mothFormatFlag.format(dateTime);
  93. mDataList.add(nowMoth);
  94. currentFlag++;
  95. }
  96. return mDataList;
  97. }
  98. ///startTime:输入其实时间的时间戳也可以。
  99. ///dayNumber:时间段
  100. ///输入时间格式
  101. List<TimeData> getTimeStartTimeAndEndTime({startTime: int, dayNumber: int, format: String}) {
  102. var mDataList = List<TimeData>();
  103. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  104. int allTimeEnd = 0;
  105. //记录当前到个数(相当于天数)
  106. int currentFlag = 0;
  107. var mothFormatFlag = new DateFormat(format);
  108. while (dayNumber >= currentFlag) {
  109. TimeData timeData=new TimeData();
  110. var dateTime = new DateTime.fromMillisecondsSinceEpoch(startTime + currentFlag * 24 * 60 * 60 * 1000);
  111. String nowMoth = mothFormatFlag.format(dateTime);
  112. timeData.dataTime=nowMoth;
  113. timeData.week=dateTime.weekday;
  114. mDataList.add(timeData);
  115. currentFlag++;
  116. }
  117. return mDataList;
  118. }
  119. ///获取某一个月的最后一天。
  120. ///我们能提供和知道的条件有:(当天的时间,)
  121. ///timeSamp:时间戳 单位(毫秒)
  122. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"
  123. getEndMoth({timeSamp: int, format: String}) {
  124. var dataFormart = new DateFormat(format);
  125. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  126. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  127. int nextTimeSamp =
  128. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  129. //取得了下一个月1号码时间戳
  130. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  131. String formartResult = dataFormart.format(dateTimeeee);
  132. return formartResult;
  133. }
  134. ///获取某一个月的最后一天。
  135. ///我们能提供和知道的条件有:(当天的时间,)
  136. ///timeSamp:传入的是时间格式
  137. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy?MM?dd hh?MM?dd" "yyyy:MM:dd"
  138. getEndMothFor({mothFormart: String, format: String}) {
  139. DateTime startData = DateTime.parse(mothFormart);
  140. var dataFormart = new DateFormat(format);
  141. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  142. startData.millisecondsSinceEpoch);
  143. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  144. int nextTimeSamp =
  145. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  146. //取得了下一个月1号码时间戳
  147. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  148. String formartResult = dataFormart.format(dateTimeeee);
  149. return formartResult;
  150. }
  151. // 获取星期
  152. static String getWeek(DateTime date){
  153. var week = date.weekday;
  154. String w = '';
  155. switch (week.toString()) {
  156. case '1':
  157. w = '一';
  158. break;
  159. case '2':
  160. w = '二';
  161. break;
  162. case '3':
  163. w = '三';
  164. break;
  165. case '4':
  166. w = '四';
  167. break;
  168. case '5':
  169. w = '五';
  170. break;
  171. case '6':
  172. w = '六';
  173. break;
  174. case '7':
  175. w = '日';
  176. break;
  177. }
  178. return '周' + w.toString();
  179. }
  180. }
  181. class TimeData{
  182. String dataTime;
  183. int week;
  184. }
  185. // ///使用:
  186. // //获取两个时间段之间的所有的日期
  187. // List<String> mdata=DateUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  188. // print(mdata.toString());
  189. // //获取从那一天开始的之后多少天之内的所有日期
  190. // List<String>mdates=DateUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  191. // print(mdates);
  192. // //通过时间戳来获取自己想要格式的日期。
  193. // DateUtils.instance.getFormartData(timeSamp:new DateTime.now().millisecondsSinceEpoch+3 * 24 * 60 * 60 * 1000,format:"yyy?MM?dd hh?MM?ss");
  194. // //获取某一个月的最后一天。传入的是时间戳(微妙)
  195. // String datad=DateUtils.instance.getEndMoth(timeSamp:new DateTime.now().millisecondsSinceEpoch-180 * 24 * 60 * 60 * 1000,format:"yyyy年MM月dd hh:MM:ss");
  196. // print(datad);
  197. // //获取某一个月的最后一天。传入的是日期(微妙)
  198. // String mothData=DateUtils.instance.getEndMothFor(mothFormart:"2019-02-11",format:"yyy年MM月dd hh?MM⏲️:ss");
  199. // print(mothData);
  200. // 结果:
  201. //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]
  202. //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]
  203. //flutter: 2019?08?04 03?08?16
  204. //flutter: 年=2019
  205. //flutter: 月=2
  206. //flutter: 2019年02月28 12:02:00
  207. //flutter: 年=2019
  208. //flutter: 月=2
  209. //flutter: 2019年02月28 12?02⏲️:00