地铁二期项目正式开始

GetDate.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var now = new Date(); //当前日期
  2. var nowDayOfWeek = now.getDay(); //今天本周的第几天
  3. var nowDay = now.getDate(); //当前日
  4. var nowMonth = now.getMonth(); //当前月
  5. var nowYear = now.getYear(); //当前年
  6. nowYear += (nowYear < 2000) ? 1900 : 0; //
  7. var lastMonthDate = new Date(); //上月日期
  8. lastMonthDate.setDate(1);
  9. lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  10. var lastYear = lastMonthDate.getYear();
  11. var lastMonth = lastMonthDate.getMonth();
  12. //格式化日期:yyyy-MM-dd
  13. function formatDate(date) {
  14. var myyear = date.getFullYear();
  15. var mymonth = date.getMonth() + 1;
  16. var myweekday = date.getDate();
  17. if (mymonth < 10) {
  18. mymonth = "0" + mymonth;
  19. }
  20. if (myweekday < 10) {
  21. myweekday = "0" + myweekday;
  22. }
  23. return (myyear + "-" + mymonth + "-" + myweekday);
  24. }
  25. //获得某月的天数
  26. function getMonthDays(myMonth) {
  27. var monthStartDate = new Date(nowYear, myMonth, 1);
  28. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  29. var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  30. return days;
  31. }
  32. //获得本季度的开始月份
  33. function getQuarterStartMonth() {
  34. var quarterStartMonth = 0;
  35. if (nowMonth < 3) {
  36. quarterStartMonth = 0;
  37. }
  38. if (2 < nowMonth && nowMonth < 6) {
  39. quarterStartMonth = 3;
  40. }
  41. if (5 < nowMonth && nowMonth < 9) {
  42. quarterStartMonth = 6;
  43. }
  44. if (nowMonth > 8) {
  45. quarterStartMonth = 9;
  46. }
  47. return quarterStartMonth;
  48. }
  49. //获得本周的开始日期
  50. function getWeekStartDate() {
  51. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek-2);
  52. return formatDate(weekStartDate);
  53. }
  54. //获得本周的结束日期
  55. function getWeekEndDate() {
  56. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (4 - nowDayOfWeek));
  57. return formatDate(weekEndDate);
  58. }
  59. //获得两周的开始日期
  60. function getTwoWeekStartDate() {
  61. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 9);
  62. return formatDate(weekStartDate);
  63. }
  64. //获得两周的结束日期
  65. function getTwoWeekEndDate() {
  66. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (4 - nowDayOfWeek));
  67. return formatDate(weekEndDate);
  68. }
  69. //获得本月的开始日期
  70. function getMonthStartDate() {
  71. var monthStartDate = new Date(nowYear, nowMonth, 1);
  72. return formatDate(monthStartDate);
  73. }
  74. //获得本月的结束日期
  75. function getMonthEndDate() {
  76. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  77. return formatDate(monthEndDate);
  78. }
  79. //获得上月开始时间
  80. function getLastMonthStartDate() {
  81. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  82. return formatDate(lastMonthStartDate);
  83. }
  84. //获得上月结束时间
  85. function getLastMonthEndDate() {
  86. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  87. return formatDate(lastMonthEndDate);
  88. }
  89. //获得本季度的开始日期
  90. function getQuarterStartDate() {
  91. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  92. return formatDate(quarterStartDate);
  93. }
  94. //或的本季度的结束日期
  95. function getQuarterEndDate() {
  96. var quarterEndMonth = getQuarterStartMonth() + 2;
  97. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  98. return formatDate(quarterStartDate);
  99. }