JavaScript gets the day, week, month, year start and end time

Source: Internet
Author: User


    1. /**
      * Get start date, end date for this week, quarter, month, month
      */
      var now = new Date (); Current date
      var nowdayofweek = Now.getday (); The day of the week today
      var nowday = Now.getdate (); Current day
      var nowmonth = Now.getmonth (); Current month
      var nowyear = Now.getyear (); When the year before
      Nowyear + = (Nowyear < 2000)? 1900:0; //


      var lastmonthdate = new Date (); Last month date
      Lastmonthdate.setdate (1);
      Lastmonthdate.setmonth (Lastmonthdate.getmonth ()-1);
      var lastyear = Lastmonthdate.getyear ();
      var lastmonth = Lastmonthdate.getmonth ();

      Formatted Date: YYYY-MM-DD
      function FormatDate (date) {
      var myyear = Date.getfullyear ();
      var mymonth = Date.getmonth () +1;
      var myweekday = Date.getdate ();

      if (Mymonth < 10) {
      Mymonth = "0" + mymonth;
      }
      if (Myweekday < 10) {
      Myweekday = "0" + myweekday;
      }
      Return (myyear+ "-" +mymonth + "-" + myweekday);
      }

      Get the number of days in a month
      function Getmonthdays (mymonth) {
      var monthstartdate = new Date (Nowyear, Mymonth, 1);
      var monthenddate = new Date (nowyear, Mymonth + 1, 1);
      var days = (monthenddate-monthstartdate)/(1000 * 60 * 60 * 24);
      return days;
      }

      Get the start month of the quarter
      function Getquarterstartmonth () {
      var quarterstartmonth = 0;
      if (nowmonth<3) {
      Quarterstartmonth = 0;
      }
      if (2<nowmonth && nowmonth<6) {
      Quarterstartmonth = 3;
      }
      if (5<nowmonth && nowmonth<9) {
      Quarterstartmonth = 6;
      }
      if (nowmonth>8) {
      Quarterstartmonth = 9;
      }
      return quarterstartmonth;
      }

      Get the start date for this week
      function Getweekstartdate () {
      var weekstartdate = new Date (nowyear, Nowmonth, Nowday-nowdayofweek);
      return FormatDate (weekstartdate);
      }

      Get the end date for this week
      function Getweekenddate () {
      var weekenddate = new Date (nowyear, Nowmonth, Nowday + (6-nowdayofweek));
      return FormatDate (weekenddate);
      }

      Get the start date for this month
      function Getmonthstartdate () {
      var monthstartdate = new Date (Nowyear, Nowmonth, 1);
      return FormatDate (monthstartdate);
      }

      Get the end date for this month
      function Getmonthenddate () {
      var monthenddate = new Date (nowyear, Nowmonth, Getmonthdays (Nowmonth));
      return FormatDate (monthenddate);
      }


      Get start time last month
      function Getlastmonthstartdate () {
      var lastmonthstartdate = new Date (Nowyear, Lastmonth, 1);
      return FormatDate (lastmonthstartdate);
      }


      Get last month end time
      function Getlastmonthenddate () {
      var lastmonthenddate = new Date (nowyear, Lastmonth, Getmonthdays (Lastmonth));
      return FormatDate (lastmonthenddate);
      }

      Get the start date for the quarter
      function Getquarterstartdate () {

      var quarterstartdate = new Date (Nowyear, Getquarterstartmonth (), 1);
      return FormatDate (quarterstartdate);
      }

      Or the end date of the quarter
      function Getquarterenddate () {
      var quarterendmonth = Getquarterstartmonth () + 2;
      var quarterstartdate = new Date (nowyear, Quarterendmonth, Getmonthdays (Quarterendmonth));
      return FormatDate (quarterstartdate);
      }

JavaScript gets the day, week, month, year start and end time

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.