Last active
August 7, 2023 04:52
-
-
Save MarketingPip/0412509960ff81a36679043fcb3f634a to your computer and use it in GitHub Desktop.
A basic function to convert years into text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numberToWords(number) { | |
const units = [ | |
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', | |
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' | |
]; | |
const tens = [ | |
'', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' | |
]; | |
if (number < 20) { | |
return units[number]; | |
} else if (number < 100) { | |
return tens[Math.floor(number / 10)] + (number % 10 !== 0 ? ` ${units[number % 10]}` : ''); | |
} else if (number < 1000) { | |
return `${units[Math.floor(number / 100)]}ty${number % 100 !== 0 ? ` and ${numberToWords(number % 100)}` : ''}`; | |
}else if (number < 2000) { | |
return `${units[Math.floor(number / 100)]} hundred${number % 100 !== 0 ? ` and ${numberToWords(number % 100)}` : ''}`; | |
} | |
} | |
function yearToText(year) { | |
if (typeof year !== 'number' || isNaN(year)) { | |
throw new Error('Input must be a valid number'); | |
} | |
if (year < 1000 || year >= 3000) { | |
throw new Error('Invalid year. Year must be between 0 and 9999'); | |
} | |
if (year < 100) { | |
return numberToWords(year); | |
} else if (year < 2000) { | |
const hundreds = Math.floor(year / 100); | |
const remainder = year % 100; | |
const and = remainder > 0 ? ' ' : ''; | |
return `${numberToWords(hundreds)} ${numberToWords(remainder)}`; | |
} else { | |
let thousands = Math.floor(year / 100); | |
const remainder = year % 1000; | |
const and = remainder > 0 ? ' and ' : ''; | |
let thousand = "" | |
if(remainder < 9 ){ | |
thousands = Math.floor(year / 1000); | |
thousand = "thousand" | |
} | |
let lastBit = numberToWords(remainder) | |
if(lastBit != "zero"){ | |
let and ="" | |
if(remainder < 9 ){ | |
and += "and " | |
} | |
lastBit = `${and}${lastBit}` | |
}else{ | |
lastBit = `` | |
} | |
return `${numberToWords(thousands)} ${thousand} ${lastBit}`; | |
} | |
} | |
console.log(yearToText(1965)); // "nineteen sixty sive" | |
console.log(yearToText(2025)); // "twenty twenty five" | |
console.log(yearToText(1979)); // "nineteen seventy nine" | |
console.log(yearToText(2000)); // "two thousand" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@spencermountain - Can't say it's common for people writing them like this. Tho - common in example "text to speech" - that they use phrases like this when speaking dates so it makes sense. Rather then "one thousand and seventy nine" //
nineteen seventy nine
.This isn't the useful function I was mentioning too! And didn't mean to tag / notify you for a garbage piece of code, tho I figured hmm maybe this would be cool to implement into Compromise.js - tho didn't know how useful it would be lol.
On that note; I'll be inviting you to the other repo which is actually useful / very cool ASAP.