Get current quarter in year with javascript

后端 未结 9 1050
有刺的猬
有刺的猬 2020-12-13 02:10

How can I get the current quarter we are in with javascript? I am trying to detect what quarter we are currently in, e.g. 2.

EDIT And how can I coun

9条回答
  •  轮回少年
    2020-12-13 02:52

    // Set Period Function
      SetPeriod(SelectedVal) {
        try {
          if (SelectedVal === '0') { return; }
          if (SelectedVal != null) {
            let yrf: number, mtf: number, dyf: number, yrt: number, mtt: number, dyt: number, dtf: any, dtt: any;
            let dat = new Date();
            let q = 0;
            switch (SelectedVal) {
              case '-1': // Not specify
                frm = ''; to = '';
                return;
              case '0': // As specify
                break;
              case '1': // This Month
                yrf = yrt = dat.getUTCFullYear();
                mtf = mtt = dat.getUTCMonth();
                dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
                break;
              case '2': // Last Month
                dat.setDate(0); // 0 will result in the last day of the previous month
                dat.setDate(1); // 1 will result in the first day of the month
                yrf = yrt = dat.getUTCFullYear();
                mtf = mtt = dat.getUTCMonth();
                dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
                break;
              case '3': // This quater
                q = Math.ceil((dat.getUTCMonth()) / 3);
              // tslint:disable-next-line:no-switch-case-fall-through
              case '4': // Last quater
                if (q === 0) { q = Math.ceil(dat.getUTCMonth() / 3) - 1; if (q === 0) { q = 1; } }
                yrf = yrt = dat.getUTCFullYear();
                if (q === 1) {
                  mtf = 0; mtt = 2;
                  dyf = 1; dyt = 31;
                } else if (q === 2) {
                  mtf = 3; mtt = 5;
                  dyf = 1; dyt = 30;
                } else if (q === 3) {
                  mtf = 6; mtt = 8;
                  dyf = 1; dyt = 30;
                } else if (q === 4) {
                  mtf = 9; mtt = 11;
                  dyf = 1; dyt = 31;
                }
                break;
              case '6': // Last year
                dat = new Date(dat.getUTCFullYear(), 0, 1);
              // tslint:disable-next-line:no-switch-case-fall-through
              case '5': // This year
                yrf = yrt = dat.getUTCFullYear();
                mtf = 0; mtt = 11;
                dyf = 1; dyt = 31;
                break;
            }
    
            // Convert to new Date
            dtf = new Date(yrf, mtf, dyf);
            dtt = new Date(yrt, mtt, dyt);
            console.log('dtf', dtf);
            console.log('dtt', dtt);
    
          }
        } catch (e) {
          alert(e);
        }
      }
    
    
    
    // Get Day in Month
      getDaysInMonth = (month: number, year: number) => {
        return new Date(year, month + 1, 0).getDate();
      }
    

提交回复
热议问题