date.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 4为 [yyyy-MM-dd,yyyy-MM-dd] 当月的第一天和最后一天
  51. * type为空默认2
  52. */
  53. export function defaultDate(type) {
  54. type = type ? type : 2
  55. const date = new Date();
  56. const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  57. const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);
  58. if (type == 1) {
  59. return [
  60. dateFormat(startDate, "yyyy-MM-dd"),
  61. dateFormat(endDate, "yyyy-MM-dd")
  62. ];
  63. }else if (type == 3){
  64. return[
  65. dateFormat(new Date(date.getFullYear(), date.getMonth()-1, 1), "yyyy-MM-dd"),
  66. dateFormat(new Date((date.getMonth() + 1) === 12?date.getFullYear()+1:date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")
  67. ];
  68. } else if (type == 4){
  69. return[
  70. dateFormat(new Date(date.getFullYear(), date.getMonth(), 1), "yyyy-MM-dd"),
  71. dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), "yyyy-MM-dd")
  72. ];
  73. }else {
  74. return [
  75. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  76. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  77. ];
  78. }
  79. }
  80. /**
  81. * 获取当天
  82. * [yyyy-MM-dd,yyyy-MM-dd]
  83. */
  84. export function defaultDate2() {
  85. const date = new Date();
  86. return [
  87. dateFormat(date, "yyyy-MM-dd"),
  88. dateFormat(date, "yyyy-MM-dd")
  89. ];
  90. }
  91. /**
  92. * 获取本月第一天和本月最后一天
  93. * [yyyy-MM-dd,yyyy-MM-dd]
  94. */
  95. export function defaultDate3() {
  96. const date = new Date();
  97. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  98. const endDate = new Date(date.getFullYear(), date.getMonth()+1, 0);
  99. return [
  100. dateFormat(startDate, "yyyy-MM-dd"),
  101. dateFormat(endDate, "yyyy-MM-dd")
  102. ];
  103. }
  104. /**
  105. * 获取当年
  106. * [yyyy-MM-dd,yyyy-MM-dd]
  107. */
  108. export function defaultDate4() {
  109. const date = new Date();
  110. return dateFormat(date, "yyyy");
  111. }
  112. /**
  113. * 获取上月第一天和上月最后一天
  114. * [yyyy-MM-dd,yyyy-MM-dd]
  115. */
  116. export function defaultDate5() {
  117. const date = new Date();
  118. const startDate = new Date(date.getFullYear(), date.getMonth()-1, 1);
  119. const endDate = new Date(date.getFullYear(), date.getMonth(), 0);
  120. return [
  121. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  122. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  123. ];
  124. }
  125. // 获得当前日期
  126. export function getCurrentDate(type = 'dateTime') {
  127. const date = new Date();
  128. const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  129. if (type == 'date') {
  130. return dateFormat(currentDate, 'yyyy-MM-dd')
  131. } else if (type == 'dateTime') {
  132. return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
  133. }
  134. }
  135. export function getYearDate(){
  136. const date = new Date();
  137. return date.getFullYear()
  138. }
  139. /**
  140. * 获取 n 天后的日期
  141. * yyyy-MM-dd
  142. */
  143. export const NdayDate = (num) => {
  144. let date1 = new Date();
  145. //今天时间
  146. let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
  147. let date2 = new Date(date1);
  148. date2.setDate(date1.getDate() + num);
  149. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  150. let time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate());
  151. return time2;
  152. }
  153. // 补零方法
  154. function addZero(num) {
  155. if (parseInt(num) < 10) {
  156. num = '0' + num;
  157. }
  158. return num;
  159. }