date.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // 获取本月开始日期和结束日期
  2. export const GetMonthDate = () => {
  3. let monthDate = {};
  4. let now = new Date(); //当前日期
  5. let nowMonth = now.getMonth(); //当前月
  6. let nowYear = now.getFullYear(); //当前年
  7. //本月的开始时间
  8. let monthStartDate = new Date(nowYear, nowMonth, 1);
  9. let yy = monthStartDate.getFullYear() + '-';
  10. let mm =
  11. (monthStartDate.getMonth() + 1 < 10
  12. ? '0' + (monthStartDate.getMonth() + 1)
  13. : monthStartDate.getMonth() + 1) + '-';
  14. let dd =
  15. monthStartDate.getDate() < 10
  16. ? '0' + monthStartDate.getDate()
  17. : monthStartDate.getDate();
  18. //本月的结束时间
  19. let monthEndDate = new Date(nowYear, nowMonth + 1, 0);
  20. let YY = monthEndDate.getFullYear() + '-';
  21. let MM =
  22. (monthEndDate.getMonth() + 1 < 10
  23. ? '0' + (monthEndDate.getMonth() + 1)
  24. : monthEndDate.getMonth() + 1) + '-';
  25. let DD =
  26. monthEndDate.getDate() < 10
  27. ? '0' + monthEndDate.getDate()
  28. : monthEndDate.getDate();
  29. monthDate.startDateString = yy + mm + dd;
  30. monthDate.endDateString = YY + MM + DD;
  31. monthDate.startDateTime = yy + mm + dd + ' 00:00:00';
  32. monthDate.endDateTime = YY + MM + DD + ' 23:59:59';
  33. monthDate.startDate = new Date(monthDate.startDateString);
  34. monthDate.endDate = new Date(monthDate.endDateString);
  35. return monthDate;
  36. };
  37. // Date转换为String
  38. // 参数:date:传入日期
  39. export const GetDateString = (date) => {
  40. let year = date.getFullYear().toString();
  41. let month = (date.getMonth() + 1 < 10
  42. ? '0' + (date.getMonth() + 1)
  43. : date.getMonth() + 1
  44. ).toString();
  45. let day = (date.getDate() < 10
  46. ? '0' + date.getDate()
  47. : date.getDate()
  48. ).toString();
  49. let hour = date.getHours().toString();
  50. let minute = date.getMinutes().toString();
  51. return year + '-' + month + '-' + day;
  52. };