getDay.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function getDay(day) {
  2. var today = new Date()
  3. var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
  4. today.setTime(targetday_milliseconds) //注意,这行是关键代码
  5. var tYear = today.getFullYear()
  6. var tMonth = today.getMonth()
  7. var tDate = today.getDate()
  8. tMonth = doHandleMonth(tMonth + 1)
  9. tDate = doHandleMonth(tDate)
  10. return tYear + '-' + tMonth + '-' + tDate
  11. }
  12. function doHandleMonth(month) {
  13. var m = month
  14. if (month.toString().length == 1) {
  15. m = '0' + month
  16. }
  17. return m
  18. }
  19. export { getDay }
  20. Date.prototype.format = function (fmt) {
  21. var o = {
  22. "M+": this.getMonth() + 1, //月份
  23. "d+": this.getDate(), //日
  24. "h+": this.getHours(), //小时
  25. "m+": this.getMinutes(), //分
  26. "s+": this.getSeconds(), //秒
  27. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  28. "S": this.getMilliseconds() //毫秒
  29. };
  30. if (/(y+)/.test(fmt)) {
  31. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  32. }
  33. for (var k in o) {
  34. if (new RegExp("(" + k + ")").test(fmt)) {
  35. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  36. }
  37. }
  38. return fmt;
  39. }
  40. export function allWeeks(now_month) {
  41. let week_array = [];
  42. let today = new Date(Date.parse(now_month));
  43. let year = today.getFullYear();
  44. let month = today.getMonth();
  45. let i = 0;
  46. let start = new Date(year, month, 1); // 得到当月第一天
  47. let end = new Date(year, month + 1, 0); // 得到当月最后一天
  48. let start_day = start.getDay(); // 当月第一天是周几
  49. console.log(start.format("yyyy-MM-dd"), end.format("yyyy-MM-dd")); // 每月的起始日期
  50. switch (start_day) {
  51. case 0:
  52. i = 0 - 1;
  53. break;
  54. case 1:
  55. i = 0 - 2;
  56. break;
  57. case 2:
  58. i = 0 - 3;
  59. break;
  60. case 3:
  61. i = 0 - 4;
  62. break;
  63. case 4:
  64. i = 0 - 5;
  65. break;
  66. case 5:
  67. i = 1;
  68. break;
  69. case 6:
  70. i = 0;
  71. break;
  72. }
  73. while (new Date(year, month, i + 6) <= end) {
  74. week_array.push([new Date(year, month, i).format("yyyy-MM-dd"),
  75. new Date(year, month, i + 6).format("yyyy-MM-dd")
  76. ])
  77. i += 7;
  78. }
  79. return week_array;
  80. }