js获取当前日期所在的周一到周日的日期,以及星期几

矫情吗;* 2023-03-01 13:27 48阅读 0赞

如图:

20200726173011786.png

  1. $(function () {
  2. var now = new Date("2020-1-1");
  3. var nowTime = now.getTime() ;
  4. var day = now.getDay() || 7;
  5. var oneDayLong = 24*60*60*1000 ;
  6. var MondayTime = nowTime - (day-1)*oneDayLong ;
  7. var SundayTime = nowTime + (7-day)*oneDayLong ;
  8. var monday = new Date(MondayTime);
  9. for (let i = 0; i<7;i++){
  10. var tmpDate = new Date(MondayTime);
  11. tmpDate=tmpDate.setDate(tmpDate.getDate()+i);
  12. tmpDate=new Date(tmpDate);
  13. console.log(tmpDate);
  14. var mondayDateStr = (tmpDate.getMonth()+1)+'月';
  15. mondayDateStr += tmpDate.getDate()+'日';
  16. let days = tmpDate.getDay();
  17. switch (days) {
  18. case 1:
  19. days = '星期一';
  20. break;
  21. case 2:
  22. days = '星期二';
  23. break;
  24. case 3:
  25. days = '星期三';
  26. break;
  27. case 4:
  28. days = '星期四';
  29. break;
  30. case 5:
  31. days = '星期五';
  32. break;
  33. case 6:
  34. days = '星期六';
  35. break;
  36. case 0:
  37. days = '星期日';
  38. break;
  39. }
  40. mondayDateStr += days;
  41. console.log(mondayDateStr) ;
  42. }
  43. var sunday = new Date(SundayTime);
  44. console.log(monday) ;
  45. console.log(sunday) ;
  46. });

发表评论

表情:
评论列表 (有 0 条评论,48人围观)

还没有评论,来说两句吧...

相关阅读