DateUtils.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. class LocalDateUtils {
  2. /**
  3. * 计算两个日期相差多少年
  4. */
  5. static int daysBetweenYear(DateTime a, DateTime b) {
  6. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  7. return v ~/ 86400000 * 30 * 12;
  8. }
  9. /**
  10. * 计算两个日期相差多少月
  11. */
  12. static int daysBetweenMonth(DateTime a, DateTime b) {
  13. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  14. return v ~/ 86400000 * 30;
  15. }
  16. /**
  17. * 计算两个日期相差多少天
  18. */
  19. static int daysBetweenDay(DateTime a, DateTime b) {
  20. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  21. return v ~/ 86400000;
  22. }
  23. /**
  24. * 计算两个日期相差多少分钟
  25. */
  26. static int daysBetweenMin(DateTime a, DateTime b) {
  27. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  28. return v ~/ 60000;
  29. }
  30. /**
  31. * 计算两个日期相差多少秒
  32. */
  33. static int daysBetweenSecond(DateTime a, DateTime b) {
  34. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  35. return v ~/ 1000;
  36. }
  37. /**
  38. * 计算两个日期相差多少毫秒
  39. */
  40. static int daysBetweenMillSecond(DateTime a, DateTime b) {
  41. int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
  42. return v;
  43. }
  44. /**
  45. * 获取当天(不足两位,拼0处理)
  46. * space 需要拼接日期的字段
  47. */
  48. static String getMMDD(DateTime dateTime, String space) {
  49. String month = dateTime.month.toString().length == 1
  50. ? "0${dateTime.month}"
  51. : dateTime.month.toString();
  52. String day = dateTime.day.toString().length == 1
  53. ? "0${dateTime.day}"
  54. : dateTime.day.toString();
  55. return "${month}${space}${day}";
  56. }
  57. /**
  58. * 获取当天(不足两位,拼0处理)
  59. * space 需要拼接日期的字段
  60. */
  61. static String getYYYYMMDD(DateTime dateTime, String space) {
  62. String year = dateTime.year.toString();
  63. String month = dateTime.month.toString().length == 1
  64. ? "0${dateTime.month}"
  65. : dateTime.month.toString();
  66. String day = dateTime.day.toString().length == 1
  67. ? "0${dateTime.day}"
  68. : dateTime.day.toString();
  69. return "${year}${space}${month}${space}${day}";
  70. }
  71. /**
  72. * 获取当天(不足两位,拼0处理)
  73. * space 需要拼接日期的字段
  74. */
  75. static String getYYYYMMDDHHMMSS(DateTime dateTime, String space) {
  76. String year = dateTime.year.toString();
  77. String month = dateTime.month.toString().length == 1
  78. ? "0${dateTime.month}"
  79. : dateTime.month.toString();
  80. String day = dateTime.day.toString().length == 1
  81. ? "0${dateTime.day}"
  82. : dateTime.day.toString();
  83. String hour = dateTime.hour.toString().length == 1
  84. ? "0${dateTime.hour}"
  85. : dateTime.hour.toString();
  86. String minute = dateTime.minute.toString().length == 1
  87. ? "0${dateTime.minute}"
  88. : dateTime.minute.toString();
  89. String second = dateTime.second.toString().length == 1
  90. ? "0${dateTime.second}"
  91. : dateTime.second.toString();
  92. return "${year}${space}${month}${space}${day} ${hour}${space}${minute}${space}${second}";
  93. }
  94. /**
  95. * 获取昨天
  96. */
  97. static String getYesterDayYYYYMMDD(DateTime dateTime) {
  98. DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
  99. dateTime.millisecondsSinceEpoch - (24 * 60 * 60 * 1000));
  100. return getYYYYMMDD(yesterDay, "-");
  101. }
  102. /**
  103. * 获取前天
  104. */
  105. static String getDayBeforeYesterdayDayYYYYMMDD(DateTime dateTime) {
  106. DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
  107. dateTime.millisecondsSinceEpoch - (2 * 24 * 60 * 60 * 1000));
  108. return getYYYYMMDD(yesterDay, "-");
  109. }
  110. /**
  111. * 获取明天
  112. */
  113. static String getTomorrowDayYYYYMMDD(DateTime dateTime) {
  114. DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
  115. dateTime.millisecondsSinceEpoch + (24 * 60 * 60 * 1000));
  116. return getYYYYMMDD(yesterDay, "-");
  117. }
  118. /**
  119. * 获取后天
  120. */
  121. static String getTomorrowAcquiredYYYYMMDD(DateTime dateTime) {
  122. DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
  123. dateTime.millisecondsSinceEpoch + (2 * 24 * 60 * 60 * 1000));
  124. return getYYYYMMDD(yesterDay, "-");
  125. }
  126. /**
  127. * 获取本周开始
  128. */
  129. static String getWeekFirstDayYYYYMMDD(DateTime dateTime) {
  130. int current = dateTime.weekday;
  131. DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
  132. dateTime.millisecondsSinceEpoch -
  133. (24 * 60 * 60 * 1000 * (current - 1)));
  134. return getYYYYMMDD(firstDay, "-");
  135. }
  136. /**
  137. * 获取本周结束
  138. */
  139. static String getWeekLastDayYYYYMMDD(DateTime dateTime) {
  140. int current = dateTime.weekday;
  141. DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
  142. dateTime.millisecondsSinceEpoch +
  143. (24 * 60 * 60 * 1000 * (7 - current)));
  144. return getYYYYMMDD(lastDay, "-");
  145. }
  146. /**
  147. * 获取上周开始
  148. */
  149. static String getLastWeekFirstDayYYYYMMDD(DateTime dateTime) {
  150. int current = dateTime.weekday;
  151. DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
  152. dateTime.millisecondsSinceEpoch -
  153. (24 * 60 * 60 * 1000 * (current - 1)));
  154. DateTime day = new DateTime.fromMillisecondsSinceEpoch(
  155. firstDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
  156. return getYYYYMMDD(day, "-");
  157. }
  158. /**
  159. * 获取上周结束
  160. */
  161. static String getLastWeekLastDayYYYYMMDD(DateTime dateTime) {
  162. int current = dateTime.weekday;
  163. DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
  164. dateTime.millisecondsSinceEpoch +
  165. (24 * 60 * 60 * 1000 * (7 - current)));
  166. DateTime day = new DateTime.fromMillisecondsSinceEpoch(
  167. lastDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
  168. return getYYYYMMDD(day, "-");
  169. }
  170. /**
  171. * 获取本月第一天
  172. */
  173. static String getMonthFirstDayYYYYMMDD(DateTime dateTime, String space) {
  174. String year = "${DateTime.now().year}";
  175. String month = "${DateTime.now().month}".length == 1
  176. ? "0${DateTime.now().month}"
  177. : "${DateTime.now().month}";
  178. return "${year}${space}${month}${space}01";
  179. }
  180. /**
  181. * 获取本月最后一天
  182. */
  183. static String getMonthLastDayYYYYMMDD(DateTime dateTime, String space) {
  184. String year = "${DateTime.now().year}";
  185. String month = "${DateTime.now().month}".length == 1
  186. ? "0${DateTime.now().month}"
  187. : "${DateTime.now().month}";
  188. int d = getDayCounts(DateTime.now().month);
  189. return "${year}${space}${month}${space}${d}";
  190. }
  191. /**
  192. * 获取周几
  193. */
  194. static String getWeekday(DateTime dateTime) {
  195. // if (dateTime == null) return null;
  196. String weekday = '星期一';
  197. switch (dateTime.weekday) {
  198. case 1:
  199. weekday = '星期一';
  200. break;
  201. case 2:
  202. weekday = '星期二';
  203. break;
  204. case 3:
  205. weekday = '星期三';
  206. break;
  207. case 4:
  208. weekday = '星期四';
  209. break;
  210. case 5:
  211. weekday = '星期五';
  212. break;
  213. case 6:
  214. weekday = '星期六';
  215. break;
  216. case 7:
  217. weekday = '星期日';
  218. break;
  219. default:
  220. break;
  221. }
  222. return weekday;
  223. }
  224. /**
  225. * 获取一个月有多少天
  226. */
  227. static int getDayCounts(int month) {
  228. int year = DateTime.now().year;
  229. int end = 0;
  230. if (month == 1) {
  231. end = 31;
  232. } else if (month == 2) {
  233. if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
  234. end = 29;
  235. } else {
  236. end = 28;
  237. }
  238. } else if (month == 3) {
  239. end = 31;
  240. } else if (month == 4) {
  241. end = 30;
  242. } else if (month == 5) {
  243. end = 31;
  244. } else if (month == 6) {
  245. end = 30;
  246. } else if (month == 7) {
  247. end = 31;
  248. } else if (month == 8) {
  249. end = 31;
  250. } else if (month == 9) {
  251. end = 30;
  252. } else if (month == 10) {
  253. end = 31;
  254. } else if (month == 11) {
  255. end = 30;
  256. } else if (month == 12) {
  257. end = 31;
  258. }
  259. return end;
  260. }
  261. }