date.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(), date.getDate()), "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 {
  68. return [
  69. dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
  70. dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
  71. ];
  72. }
  73. }
  74. /**
  75. * 获取当天
  76. * [yyyy-MM-dd,yyyy-MM-dd]
  77. */
  78. export function defaultDate2() {
  79. const date = new Date();
  80. return [
  81. dateFormat(date, "yyyy-MM-dd"),
  82. dateFormat(date, "yyyy-MM-dd")
  83. ];
  84. }
  85. /**
  86. * 获取本月第一天和本月最后一天
  87. * [yyyy-MM-dd,yyyy-MM-dd]
  88. */
  89. export function defaultDate3() {
  90. const date = new Date();
  91. const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
  92. const endDate = new Date(date.getFullYear(), date.getMonth()+1, 0);
  93. return [
  94. dateFormat(startDate, "yyyy-MM-dd"),
  95. dateFormat(endDate, "yyyy-MM-dd")
  96. ];
  97. }
  98. // 获得当前日期
  99. export function getCurrentDate(type = 'dateTime') {
  100. const date = new Date();
  101. const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  102. if (type == 'date') {
  103. return dateFormat(currentDate, 'yyyy-MM-dd')
  104. } else if (type == 'dateTime') {
  105. return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
  106. }
  107. }
  108. export function getYearDate(){
  109. const date = new Date();
  110. return date.getFullYear()
  111. }