How to get century from year in TypeScript

1 Answer

0 votes
function getCenturyFromYear(year : number) : number {
  	return Math.floor((year - 1) / 100) + 1;
}

const arr = [100, 101, 310, 580, 1000, 1008, 1999, 2000, 2001, 2022];

arr.forEach(function(yr) {
  	console.log('year ' + yr + ' is in ' + getCenturyFromYear(yr) + ' century');
});


  
  
  
  
/*
run:
  
"year 100 is in 1 century" 
"year 101 is in 2 century" 
"year 310 is in 4 century" 
"year 580 is in 6 century" 
"year 1000 is in 10 century" 
"year 1008 is in 11 century" 
"year 1999 is in 20 century" 
"year 2000 is in 20 century" 
"year 2001 is in 21 century" 
"year 2022 is in 21 century" 
  
*/
   
   

 



answered May 5, 2022 by avibootz
...