Skip to content

Instantly share code, notes, and snippets.

@sandeepsuvit
Created August 17, 2018 07:09
Show Gist options
  • Save sandeepsuvit/ca1450bbe0fd54b24d2f85ee1e3ed5ba to your computer and use it in GitHub Desktop.
Save sandeepsuvit/ca1450bbe0fd54b24d2f85ee1e3ed5ba to your computer and use it in GitHub Desktop.
Get decades from year Typescript, JavaScript
// Set of utility functions
export const CommonUtils = {
/**
* Get the decades from year
* =========================================================================
* This utility will get the nearest decades of a year.<br/>
* example: getDecadesFromYear(1965) -> { start: 1960, end: 1970 }
* <br/>
*
* @return {[type]} [description]
*/
getDecadesFromYear: function(year: number): any {
if (Number.isNaN(year) || (year.toString().length < 4) || (year.toString().length > 4)) {
throw new Error('Date must be valid and have a 4-digit year attribute');
}
let start = Number(`${year.toString()[2]}0`);
let startIdx = year.toString().substring(0, 2);
let end = 0;
start = (start === 0) ? Number(`${startIdx}00`) : Number(`${startIdx}${start}`);
end = start + 10;
return { start: start, end: end };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment