简体   繁体   中英

Last day of selectected quarter

I am using the following function to find the last day of quarter of selected day. Now I am just adding 12*Date.WEEK but is not exactly what I want. Could you please have look and advice how to find the last day of quarter? I want to have the last day of quarter in date2.

 function catcalc(cal) { 
 var date = cal.date;
    var time = date.getTime(); 
    var field = document.getElementById("x_Bis");
    if (field == cal.params.inputField) {        
        field = document.getElementById("x_Von");
        time -= 12*Date.WEEK; // substract one week
    } else {                           
     time += 12*Date.WEEK; // add one week  6*Date.DAY
    }                                        
    var date2 = new Date(time);                              
  field.value = date2.print("%d.%m.%Y");  

I should keep the function structure and keep the name of variable. There are two calendar fields which are linked to each others. The user can just select from calendar any date (no clock time is necessary). The function is coming from http://cimanet.uoc.edu/logica/v2/lib/jscalendar-1.0/doc/html/reference.html .

Live Demo

function getQ(date) {
  var t=new Date(date.getTime()),year=t.getFullYear() // copy
  t.setHours(0,0,0,0); // normalise to not get boundary errors
  // note months start at 0 in  JS
  var q1 = new Date(year,2,31),
      q2 = new Date(year,5,30),
      q3 = new Date(year,8,30),
      q4 = new Date(year,11,31);
  if (t<=q1) return q1;
  if (t<=q2) return q2;
  if (t<=q3) return q3;
  if (t<=q4) return q4;
}
getQ(new Date()); // today will return 31st of March

In your code, I would guess you can use

function catcalc(cal) { 
  var date = cal.date;
  var field = document.getElementById("x_Bis");
  if (field == cal.params.inputField) {        
    field = document.getElementById("x_Von");
  }                                        
  var date2 = getQ(date);
  field.value = date2.print("%d.%m.%Y");  
}

Expanding @mplungjan proposal (and minding the JavaScript dumbness when working with dates). I'll use method chaining, so every next method will operate on what previous method have returned.

function getLastDayOfQuarter(date) {
  var year = date.getFullYear();
  var quarterEndings = [[3, 31], [6, 30], [9, 30], [12, 31]];

  var toDateObj = function (dates) {
    return new Date(year, dates[0] - 1, dates[1]);
  };

  var isBeforeEndDate = function (endDate) {
    return endDate >= date;
  }

  date.setHours(0, 0, 0, 0);

  return quarterEndings
    .map(toDateObj)
    .filter(isBeforeEndDate)[0];
}

getLastDayOfQuarter(new Date()).toDateString(); // "Mon Mar 31 2014"

Demo: http://jsbin.com/eZoliye/1/edit?js,console

This may be simpler than other answers. Gives you first and last date of current quarter. Works with other arbitrary dates if you change the first line.

let now = new Date();
let quarter = Math.floor((now.getMonth() / 3));
let startDate = new Date(now.getFullYear(), quarter * 3, 1);
let endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 3, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM