getDay.js 2.5 KB

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