JS get this week, this quarter, this month, the last month's start date, end date

Source: Internet
Author: User

  1. /**
  2. * Get start date, end date for this week, quarter, month, month
  3. */
  4. var now = new Date (); //Current date
  5. var nowdayofweek = Now.getday (); //The day of the week today
  6. var nowday = Now.getdate (); //Current day
  7. var nowmonth = Now.getmonth (); //Current Month
  8. var nowyear = Now.getyear (); //When the year before
  9. Nowyear + = (Nowyear < 2000)?  1900:0; //    
  10. var lastmonthdate = new Date (); //Last month date
  11. Lastmonthdate.setdate (1);
  12. Lastmonthdate.setmonth (Lastmonthdate.getmonth ()-1);
  13. var lastyear = Lastmonthdate.getyear ();
  14. var lastmonth = Lastmonthdate.getmonth ();
  15. Formatted Date: YYYY-MM-DD
  16. function FormatDate (date) {
  17. var myyear = Date.getfullyear ();
  18. var mymonth = Date.getmonth () +1;
  19. var myweekday = Date.getdate ();
  20. if (Mymonth <) {
  21. Mymonth = "0" + mymonth;
  22. }
  23. if (Myweekday <) {
  24. Myweekday = "0" + myweekday;
  25. }
  26. return (myyear+"-" +mymonth + "-" + myweekday);
  27. }
  28. Get the number of days in a month
  29. function Getmonthdays (mymonth) {
  30. var monthstartdate = New Date (Nowyear, Mymonth, 1);
  31. var monthenddate = New Date (nowyear, Mymonth + 1, 1);
  32. var days = (monthenddate-monthstartdate)/(1000 * 60 * 60 * 24);
  33. return days;
  34. }
  35. Get the start month of the quarter
  36. function Getquarterstartmonth () {
  37. var quarterstartmonth = 0;
  38. if (nowmonth<3) {
  39. Quarterstartmonth = 0;
  40. }
  41. if (2<nowmonth && nowmonth<6) {
  42. Quarterstartmonth = 3;
  43. }
  44. if (5<nowmonth && nowmonth<9) {
  45. Quarterstartmonth = 6;
  46. }
  47. if (nowmonth>8) {
  48. Quarterstartmonth = 9;
  49. }
  50. return quarterstartmonth;
  51. }
  52. Get the start date for this week
  53. function Getweekstartdate () {
  54. var weekstartdate = New Date (Nowyear, Nowmonth, Nowday-nowdayofweek);
  55. return FormatDate (weekstartdate);
  56. }
  57. Get the end date for this week
  58. function Getweekenddate () {
  59. var weekenddate = New Date (Nowyear, Nowmonth, Nowday + (6-nowdayofweek));
  60. return FormatDate (weekenddate);
  61. }
  62. Get the start date for this month
  63. function Getmonthstartdate () {
  64. var monthstartdate = New Date (Nowyear, Nowmonth, 1);
  65. return FormatDate (monthstartdate);
  66. }
  67. Get the end date for this month
  68. function Getmonthenddate () {
  69. var monthenddate = New Date (Nowyear, Nowmonth, Getmonthdays (Nowmonth));
  70. return FormatDate (monthenddate);
  71. }
  72. Get start time last month
  73. function Getlastmonthstartdate () {
  74. var lastmonthstartdate = New Date (Nowyear, Lastmonth, 1);
  75. return FormatDate (lastmonthstartdate);
  76. }
  77. Get last month end time
  78. function Getlastmonthenddate () {
  79. var lastmonthenddate = New Date (Nowyear, Lastmonth, Getmonthdays (Lastmonth));
  80. return FormatDate (lastmonthenddate);
  81. }
  82. Get the start date for the quarter
  83. function Getquarterstartdate () {
  84. var quarterstartdate = New Date (Nowyear, Getquarterstartmonth (), 1);
  85. return FormatDate (quarterstartdate);
  86. }
  87. Or the end date of the quarter
  88. function Getquarterenddate () {
  89. var quarterendmonth = getquarterstartmonth () + 2;
  90. var quarterstartdate = New Date (Nowyear, Quarterendmonth, Getmonthdays (Quarterendmonth));
  91. return FormatDate (quarterstartdate);
  92. }

JS get this week, this quarter, this month, the last month's start date, end date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.