Javascript calculate billable days in each quarter and how many days are remaining

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I came across this javascript on stack overflow that will calculate the billable days in each quarter and how many days are remaining in the quarter. I am not sure how to edit this script to work in Skuid. I am hoping someone can help me to get it working. Thank You!

What Quarter we are in:
function getQuarter(d) {
  d = d || new Date(); // If no date supplied, use today
  var q = [4,1,2,3];
  return q[Math.floor(d.getMonth() / 3)];
}
How many days remaining in Quarter:

function daysLeftInQuarter(d) {
d = d || new Date();
var qEnd = new Date(d);
qEnd.setMonth(qEnd.getMonth() + 3 - qEnd.getMonth() % 3, 0);
return Math.floor((qEnd - d) / 8.64e7);
}
Photo of Tami Lust

Tami Lust

  • 5,280 Points 5k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Others smarter than me may have a better way of going about this,  but here are some answers. 

Basically what you have to do is give this code a context where it runs.  This context needs data being evaluating and a field in the UI that is being updated.  The easiest way to do that is by dragging a standard salesforce field onto your page and then building a custom Field Renderer for it. 

I used the create date field. 

Then you create snippets.  I took what you provided and broke them out of the functions so they run more directly.  .   

Here is the current quarter: 

var $ = skuid.$,    field = arguments[0],
    date = arguments[1],
    jsdate = skuid.time.parseSFDate(date)|| new Date(); // If no date supplied, use today
    var q = [1,2,3,4];
// quarter definition:  1: Jan - Mar  2: Apr - Jun  3: Jul - Sep 4: Oct - Dec    
    
    var newValue=q[Math.floor(jsdate.getMonth() / 3)];
//console.log (jsdate,newValue);
skuid.ui.fieldRenderers.TEXT[field.mode](field, newValue);

Quarter definition threw me for a loop until I found the original stack exchange post where you got the code.  The definition used there was US Goft fiscal year where Q1 = October - December... DOH

Then: 

Here is days remaining in quarter: 

var $ = skuid.$,field = arguments[0],
date = new Date();
var qEnd = new Date(date);
qEnd.setMonth(qEnd.getMonth() + 3 - qEnd.getMonth() % 3, 0);
var newValue = Math.floor((qEnd - date) / 8.64e7);
//console.log (qEnd,newValue);
skuid.ui.fieldRenderers.TEXT[field.mode](field, newValue);

Enjoy! 
Photo of Tami Lust

Tami Lust

  • 5,280 Points 5k badge 2x thumb
Thank you Rob! I will give a go and report back.