dateUtils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export default {
  2. getBeforeDate: function (beforeDayNumber) {
  3. /**
  4. * 获取当前日前多少天
  5. * 返回格式: YYYY-mm-dd
  6. * 例子: 当日为: 2020-11-27
  7. * 返回日期为: 2020-11-01
  8. * */
  9. let n = beforeDayNumber;
  10. let d = new Date();
  11. let year = d.getFullYear();
  12. let month = d.getMonth() + 1;
  13. let day = d.getDate();
  14. if(day <= n) {
  15. if(month > 1) {
  16. month = month - 1;
  17. } else {
  18. year = year - 1;
  19. month = 12;
  20. }
  21. }
  22. d.setDate(d.getDate() - n);
  23. year = d.getFullYear();
  24. month = d.getMonth() + 1;
  25. day = d.getDate();
  26. return year + "/" + (month < 10 ? ('0' + month) : month) + "/" + (day < 10 ? ('0' + day) : day);
  27. },
  28. getCurrentMonthFirstDay: function () {
  29. /**
  30. * 获取当前月的第一天
  31. * 返回格式: YYYY-mm-dd
  32. * 例子: 当日为: 2020-11-27
  33. * 返回日期为: 2020-11-01
  34. * */
  35. let date = new Date();
  36. date.setDate(1);
  37. let month = date.getMonth() + 1;
  38. let day = date.getDate();
  39. if (month < 10) {
  40. month = '0' + month
  41. }
  42. if (day < 10) {
  43. day = '0' + day
  44. }
  45. return date.getFullYear() + '/' + month + '/' + day;
  46. },
  47. getCurrentMonthLastDay: function () {
  48. /**
  49. * 获取当前月的最后一天
  50. * 返回格式: YYYY-mm-dd
  51. * 例子: 当日为: 2020-11-27
  52. * 返回日期为: 2020-11-30
  53. * */
  54. let date = new Date();
  55. let currentMonth = date.getMonth();
  56. let nextMonth = ++currentMonth;
  57. let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  58. let oneDay = 1000 * 60 * 60 * 24;
  59. let lastTime = new Date(nextMonthFirstDay - oneDay);
  60. let month = lastTime.getMonth() + 1;
  61. let day = lastTime.getDate();
  62. if (month < 10) {
  63. month = '0' + month
  64. }
  65. if (day < 10) {
  66. day = '0' + day
  67. }
  68. return date.getFullYear() + '/' + month + '/' + day;
  69. },
  70. formatDate: function (date) {
  71. let month = date.getMonth() + 1;
  72. let day = date.getDate();
  73. if(month < 10) {
  74. month = '0' + month;
  75. }
  76. if(day < 10) {
  77. day = '0' + day;
  78. }
  79. return `${date.getFullYear()}/${month}/${day}`;
  80. },
  81. }