Hibok
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

139 řádky
6.8 KiB

  1. import 'package:intl/intl.dart';
  2. class DateUtils {
  3. // 工厂模式
  4. factory DateUtils() => _getInstance();
  5. static DateUtils get instance => _getInstance();
  6. static DateUtils _instance;
  7. DateUtils._internal() {
  8. // 初始化
  9. }
  10. static DateUtils _getInstance() {
  11. if (_instance == null) {
  12. _instance = new DateUtils._internal();
  13. }
  14. return _instance;
  15. }
  16. ///将时间日期格式转化为时间戳
  17. ///2018年12月11日
  18. ///2019-12-11
  19. ///2018年11月15 11:14分89
  20. ///结果是毫秒
  21. int getTimeStap({formartData: String}) {
  22. var result = formartData.substring(0, 4) + "-" + formartData.substring(5, 7) + "-" + formartData.substring(8, 10);
  23. if (formartData.toString().length>=13&&formartData.substring(10, 13) != null) {
  24. result += "" + formartData.substring(10, 13);
  25. }
  26. if (formartData.toString().length>=17&&formartData.toString().substring(14, 16) != null) {
  27. result += ":" + formartData.substring(14, 16);
  28. }
  29. if (formartData.toString().length>=19&&formartData.substring(17, 19) != null) {
  30. result += ":" + formartData.substring(17, 19);
  31. }
  32. var dataTime = DateTime.parse(result);
  33. print(dataTime.millisecondsSinceEpoch);
  34. return dataTime.millisecondsSinceEpoch;
  35. }
  36. ///1.获取从某一天开始到某一天结束的所有的中间日期,例如输入 startTime:2019:07:31 endTime:2019:08:31 就会返回所有的中间天数。
  37. ///startTime和endTime格式如下都可以
  38. ///使用: List<String> mdata=DataUtils.instance.getTimeBettwenStartTimeAndEnd(startTime:"2019-07-11",endTime:"2019-08-29",format:"yyyy年MM月dd");
  39. ///结果:[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]
  40. List<String> getTimeBettwenStartTimeAndEnd(
  41. {startTime: String, endTime: String, format: String}) {
  42. var mDataList = List<String>();
  43. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  44. int allTimeEnd = 0;
  45. //记录当前到个数(相当于天数)
  46. int currentFlag = 0;
  47. DateTime startData = DateTime.parse(startTime);
  48. DateTime endData = DateTime.parse(endTime);
  49. var mothFormatFlag = new DateFormat(format);
  50. while (endData.millisecondsSinceEpoch > allTimeEnd) {
  51. allTimeEnd =
  52. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000;
  53. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  54. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  55. String nowMoth = mothFormatFlag.format(dateTime);
  56. mDataList.add(nowMoth);
  57. currentFlag++;
  58. }
  59. return mDataList;
  60. }
  61. ///传入starTime格式 2012-02-27 13:27:00 或者 "2012-02-27等....
  62. ///dayNumber:从startTime往后面多少天你需要输出
  63. ///formart:获取到的日期格式。"yyyy年MM月dd" "yyyy-MM-dd" "yyyy年" "yyyy年MM月" "yyyy年\nMM月dd" 等等
  64. ///使用:DataUtils.instance.getTimeStartTimeAndEnd(startTime:"2019-07-11",dayNumber:10,format:"yyyy年MM月dd");
  65. ///结果:[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]
  66. List<String> getTimeStartTimeAndEnd(
  67. {startTime: String, dayNumber: int, format: String}) {
  68. var mDataList = List<String>();
  69. //记录往后每一天的时间搓,用来和最后一天到做对比。这样就能知道什么时候停止了。
  70. //记录当前到个数(相当于天数)
  71. int currentFlag = 0;
  72. DateTime startData = DateTime.parse(startTime);
  73. var mothFormatFlag = new DateFormat(format);
  74. while (dayNumber >= currentFlag) {
  75. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  76. startData.millisecondsSinceEpoch + currentFlag * 24 * 60 * 60 * 1000);
  77. String nowMoth = mothFormatFlag.format(dateTime);
  78. mDataList.add(nowMoth);
  79. currentFlag++;
  80. }
  81. return mDataList;
  82. }
  83. ///格式化时间戳
  84. ///timeSamp:毫秒值
  85. ///format:"yyyy年MM月dd hh:mm:ss" "yyy😄MM👌dd hh🙅MM🐶dd" "yyyy:MM:dd"......
  86. ///结果: 2019😄08👌04 02🙅08🐶02
  87. getFormartData({timeSamp: int, format: String}) {
  88. var dataFormart = new DateFormat(format);
  89. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  90. String formartResult = dataFormart.format(dateTime);
  91. return formartResult;
  92. }
  93. ///获取某一个月的最后一天。
  94. ///我们能提供和知道的条件有:(当天的时间,)
  95. ///timeSamp:时间戳 单位(毫秒)
  96. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy😄MM👌dd hh🙅MM🐶dd" "yyyy:MM:dd"
  97. getEndMoth({timeSamp: int, format: String}) {
  98. var dataFormart = new DateFormat(format);
  99. var dateTime = new DateTime.fromMillisecondsSinceEpoch(timeSamp);
  100. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  101. int nextTimeSamp =
  102. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  103. //取得了下一个月1号码时间戳
  104. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  105. String formartResult = dataFormart.format(dateTimeeee);
  106. return formartResult;
  107. }
  108. ///获取某一个月的最后一天。
  109. ///我们能提供和知道的条件有:(当天的时间,)
  110. ///timeSamp:时间戳 单位(毫秒)
  111. ///format:想要的格式 "yyyy年MM月dd hh:mm:ss" "yyy😄MM👌dd hh🙅MM🐶dd" "yyyy:MM:dd"
  112. getEndMothFor({mothFormart: String, format: String}) {
  113. DateTime startData = DateTime.parse(mothFormart);
  114. var dataFormart = new DateFormat(format);
  115. var dateTime = new DateTime.fromMillisecondsSinceEpoch(
  116. startData.millisecondsSinceEpoch);
  117. var dataNextMonthData = new DateTime(dateTime.year, dateTime.month + 1, 1);
  118. int nextTimeSamp =
  119. dataNextMonthData.millisecondsSinceEpoch - 24 * 60 * 60 * 1000;
  120. //取得了下一个月1号码时间戳
  121. var dateTimeeee = new DateTime.fromMillisecondsSinceEpoch(nextTimeSamp);
  122. String formartResult = dataFormart.format(dateTimeeee);
  123. return formartResult;
  124. }
  125. }