date.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. export const calcDate = (date1, date2) => {
  2. let date3 = date2 - date1;
  3. let days = Math.floor(date3 / (24 * 3600 * 1000))
  4. let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  5. let hours = Math.floor(leave1 / (3600 * 1000))
  6. let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  7. let minutes = Math.floor(leave2 / (60 * 1000))
  8. let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  9. let seconds = Math.round(date3 / 1000)
  10. return {
  11. leave1,
  12. leave2,
  13. leave3,
  14. days: days,
  15. hours: hours,
  16. minutes: minutes,
  17. seconds: seconds,
  18. }
  19. }
  20. /**
  21. * 日期格式化
  22. */
  23. export function dateFormat(date, format) {
  24. format = format || 'yyyy-MM-dd hh:mm:ss';
  25. if (date !== 'Invalid Date') {
  26. let o = {
  27. "M+": date.getMonth() + 1, //month
  28. "d+": date.getDate(), //day
  29. "h+": date.getHours(), //hour
  30. "m+": date.getMinutes(), //minute
  31. "s+": date.getSeconds(), //second
  32. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  33. "S": date.getMilliseconds() //millisecond
  34. }
  35. if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  36. (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  37. for (let k in o)
  38. if (new RegExp("(" + k + ")").test(format))
  39. format = format.replace(RegExp.$1,
  40. RegExp.$1.length === 1 ? o[k] :
  41. ("00" + o[k]).substr(("" + o[k]).length));
  42. return format;
  43. }
  44. return '';
  45. }
  46. /**
  47. * 获取上月第一天和下月最后一天
  48. * type 1为 [yyyy-MM-dd,yyyy-MM-dd]
  49. * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59]
  50. * type为空默认2
  51. */
  52. export function defaultDate(type) {
  53. type = type ? type : 2
  54. const date = new Date();
  55. const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  56. const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);
  57. if (type == 1) {
  58. return [
  59. dateFormat(startDate, "yyyy-MM-dd"),
  60. dateFormat(endDate, "yyyy-MM-dd")
  61. ];
  62. }else if (type == 3){
  63. return[
  64. dateFormat(new Date(date.getFullYear(), date.getMonth()-1, 1), "yyyy-MM-dd"),
  65. dateFormat(new Date((date.getMonth() + 1) === 12?date.getFullYear()+1:date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")
  66. ];
  67. } else if (type == 4){
  68. return[
  69. dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"),
  70. dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd")
  71. ];
  72. }else {
  73. return [
  74. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  75. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  76. ];
  77. }
  78. }
  79. /**
  80. * 获取当天
  81. * [yyyy-MM-dd,yyyy-MM-dd]
  82. */
  83. export function defaultDate2() {
  84. const date = new Date();
  85. return [
  86. dateFormat(date, "yyyy-MM-dd"),
  87. dateFormat(date, "yyyy-MM-dd")
  88. ];
  89. }
  90. /**
  91. * 获取本月第一天和本月最后一天
  92. * [yyyy-MM-dd,yyyy-MM-dd]
  93. */
  94. export function defaultDate3() {
  95. const date = new Date();
  96. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  97. const endDate = new Date(date.getFullYear(), date.getMonth()+1, 0);
  98. return [
  99. dateFormat(startDate, "yyyy-MM-dd"),
  100. dateFormat(endDate, "yyyy-MM-dd")
  101. ];
  102. }
  103. /**
  104. * 获取当年
  105. * [yyyy-MM-dd,yyyy-MM-dd]
  106. */
  107. export function defaultDate4() {
  108. const date = new Date();
  109. return dateFormat(date, "yyyy");
  110. }
  111. /**
  112. * 获取上月第一天和上月最后一天
  113. * [yyyy-MM-dd,yyyy-MM-dd]
  114. */
  115. export function defaultDate5() {
  116. const date = new Date();
  117. const startDate = new Date(date.getFullYear(), date.getMonth()-1, 1);
  118. const endDate = new Date(date.getFullYear(), date.getMonth(), 0);
  119. return [
  120. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  121. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  122. ];
  123. }
  124. // 获得当前日期
  125. export function getCurrentDate(type = 'dateTime') {
  126. const date = new Date();
  127. const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  128. if (type == 'date') {
  129. return dateFormat(currentDate, 'yyyy-MM-dd')
  130. } else if (type == 'dateTime') {
  131. return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
  132. }
  133. }
  134. export function getYearDate(){
  135. const date = new Date();
  136. return date.getFullYear()
  137. }