How to get century from year in JavaScript

1 Answer

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

const arr = [100, 101, 320, 590, 1000, 1007, 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 320 is in 4 century"
"year 590 is in 6 century"
"year 1000 is in 10 century"
"year 1007 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
edited May 5, 2022 by avibootz
...