utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import dayjs from 'dayjs'
  2. export const formatData = (times, keys, format = 'YYYY-MM-DD') => {
  3. const data = {}
  4. if (times && times.length) {
  5. for (let i = 0; i < keys.length; i++) {
  6. data[keys[i]] = dayjs(times[i]).format(format)
  7. }
  8. }
  9. return data
  10. }
  11. export const getNowDateAndMonday=(time)=> {
  12. let timestamp = new Date(time.replace(/-/g, "/")).getTime();
  13. let serverDate = new Date(time);
  14. if (serverDate.getDay() == 0) {
  15. timestamp -= 7 * 24 * 60 * 60 * 1000;
  16. }
  17. let mondayTime =
  18. timestamp - (serverDate.getDay() - 1) * 24 * 60 * 60 * 1000;
  19. let mondayData = new Date(mondayTime);
  20. //年
  21. let mondayY = mondayData.getFullYear();
  22. //月
  23. let mondayM =
  24. mondayData.getMonth() + 1 < 10
  25. ? "0" + (mondayData.getMonth() + 1)
  26. : mondayData.getMonth() + 1;
  27. //日
  28. let mondayD =
  29. mondayData.getDate() < 10
  30. ? "0" + mondayData.getDate()
  31. : mondayData.getDate();
  32. let str = mondayY + "-" + mondayM + "-" + mondayD;
  33. return str;
  34. }
  35. export const getNowDateAndSunday=(time) =>{
  36. let timestamp = new Date(time.replace(/-/g, "/")).getTime();
  37. let serverDate = new Date(time);
  38. let num = 7 - serverDate.getDay();
  39. if (num == 7) {
  40. num = 0;
  41. }
  42. let sundayTiem = timestamp + num * 24 * 60 * 60 * 1000;
  43. let SundayData = new Date(sundayTiem);
  44. //年
  45. let tomorrowY = SundayData.getFullYear(); //月
  46. let tomorrowM =
  47. SundayData.getMonth() + 1 < 10
  48. ? "0" + (SundayData.getMonth() + 1)
  49. : SundayData.getMonth() + 1;
  50. //日
  51. let tomorrowD =
  52. SundayData.getDate() < 10
  53. ? "0" + SundayData.getDate()
  54. : SundayData.getDate();
  55. let str = tomorrowY + "-" + tomorrowM + "-" + tomorrowD;
  56. return str;
  57. }