Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Last active August 7, 2023 04:52
Show Gist options
  • Save MarketingPip/0412509960ff81a36679043fcb3f634a to your computer and use it in GitHub Desktop.
Save MarketingPip/0412509960ff81a36679043fcb3f634a to your computer and use it in GitHub Desktop.
A basic function to convert years into text.
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"
@MarketingPip
Copy link
Author

This code is junky & there are much better ways of doing this. But! Hope this serve's someone.

@MarketingPip
Copy link
Author

@spencermountain - this code is garbage. And I should have just used Compromise. But - check this out. Maybe we could add something like this to the library.

Assuming just some splitting and .numbers() etc with some basic logic could handle it. And possibly a reverse function. Like console.log(textToYear("twenty twenty five")); // 2025

@spencermountain
Copy link

spencermountain commented Aug 4, 2023

hey Jared - that's neat! Ha, that's all compromise does.
Man, who writes dates like that. ... only wikipedia editors.
Let me know if I can help.

@MarketingPip
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment