Monday, September 19, 2011

Servoy TIP: Getting The Current Quarter Date Range

I do a lot of applications where users want reports or lists that take a date in the current record and filter or find all the records "In This Quarter".

The calculation isn't all that difficult - but it's a cool time saver. Below, I've actually created 3 functions - so you can use the quarter start date, end date, or date range.


/**
 * Return the starting quarter date for a given date
 * 
 * @param {Date} inputDate
 * @return {Date}
 */
function getQuarterStartDate(inputDate) {
if(!inputDate) { return null; }
var inDate = new Date(inputDate);
var mo = inDate.getMonth() + 1; //remember JS months are 0-11!
var yr = inDate.getFullYear();
if(mo > 9) {
return new Date(yr, 10, 1);
} else if (mo > 6) {
return new Date(yr, 7, 1);
}  else if (mo > 3) {
return new Date(yr, 4, 1);
} else {
return new Date(yr, 1, 1);
}
}


/**
 * Return the ending quarter date for a given date
 * 
 * @param {Date} inputDate
 * @return {Date}
 */
function getQuarterEndDate(inputDate) {
if(!inputDate) { return null; }
var inDate = new Date(inputDate);
var mo = inDate.getMonth() + 1; //remember JS months are 0-11!
var yr = inDate.getFullYear();
if(mo > 9) {
return new Date(yr, 12, 31);
} else if (mo > 6) {
return new Date(yr, 9, 30);
}  else if (mo > 3) {
return new Date(yr, 6, 30);
} else {
return new Date(yr, 3, 31);
}
}

/**
 * Return a search string properly formatted for the start/end quarter dates
 * 
 * @param {Date} inputDate
 * @return {String}
 */
function getQuarterRange(inputDate) {
if(!inputDate) { return null; }
var sDate = getQuarterStartDate(inputDate);
var eDate = getQuarterEndDate(inputDate);
var theFormat = i18n.getDefaultDateFormat();
if(sDate && eDate) {
return utils.dateFormat(sDate,theFormat) +
"..." + utils.dateFormat(sDate,theFormat) + "|" + theFormat;
} else {
return null;
}
}

Monday, September 12, 2011

Servoy TIP: Changing A Form's Style On-The-Fly!

I had a need the other day to change the style that was displaying on a form. My first thought was to just change all the object properties via scripting (a HUGE pain!). But, with the help of the SolutionModel (and controller.recreateUI() - in version 5.2+) - it was a snap.

Let's say you have two styles: "defaultStyle" and "fancyStyle" defined in your resources. If you use this code:
var myForm = solutionModel.getForm(controller.getName());
myForm.styleClass = "fancyForm";
controller.recreateUI();
Done! Be careful here - depending on how you designed your styles - things could get "wonky" (margins are the big culprit)... but man, did I mention how much I LOVE this tool?!

Friday, September 2, 2011

Servoy TIP: How To Create a Number Suffix

Here's a tip for adding the number suffix - like 1st, 2nd, 3rd, 4th, etc:

/**
 * @param {Number} input - the number to get the suffix for
 * @param {Number} returnWholeString - values: 0 (or null) - just the suffix, 1 (or >0) - number and suffix
 */
function getNumberSuffix(input,returnWholeString) {
if(returnWholeString == null || returnWholeString == undefined || returnWholeString == 0) {
returnWholeString = 0;
} else {
returnWholeString = 1;
}

var output = "th"
var last = 0;

if(input%100 >= 11 && input%100 <= 13) {
output = "th"
} else {
last = Math.ceil(input%10);
if(last == 1) {
 output = "st";
} else if(last == 2) {
output = "nd";
} else if(last == 3) {
output = "rd";
}
}

if(returnWholeString) {
return input + output;
} else {
return output;
}
}

Example usage:

getNumberSuffix(21) returns "st"
getNumberSuffix(21,1) returns "21st"

getNumberSuffix(13) returns "th"
getNumberSuffix(13,1) returns "13th"


Pretty simple - but powerful!