Home > Software design >  Outputting dates in natural language in javascript
Outputting dates in natural language in javascript

Time:01-25

My friend has asked me to do what is apparently a giant pain, which is to convert dates from a date picker into natural language in first english then also french because he is a notary who wants a tool to copy/paste natural dates into his documents so he doesn't make errors.

The desired output format would be: twenty-fourth day of January two thousand and twenty-two

I've gone through about 8 existing libraries but none seem to do calendar day or year in natural language.

CodePudding user response:

The way to do this would be to convert each entity of date separately. (The answer uses a reference to Transform numbers to words in lakh / crore system to convert year to words which is modified to your use case.)

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];



function yearInWords (num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000'   num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return; var str = '';
    str  = (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]]   ' '   a[n[1][1]])   'crore ' : '';
    str  = (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]]   ' '   a[n[2][1]])   'lakh ' : '';
    str  = (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]]   ' '   a[n[3][1]])   'thousand ' : '';
    str  = (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]]   ' '   a[n[4][1]])   'hundred ' : '';
    str  = (n[5] != 0) ? ((str != '') ? 'and ' : '')   (a[Number(n[5])] || b[n[5][0]]   (a[n[5][1]] ? '-' : '')   a[n[5][1]]) : '';
    return str;
}

 var days = ['first','second','third','fourth','fifth', 'sixth','seventh','eigth','ninth','tenth','eleventh','twelth','thirteenth','fouteenth','fifteenth','sixteenth','seventeen',
 'eighteenth','nineteenth', 'twentieth', 'twenty', 'thirtieth', 'thirty'];

 function dayInWords (num) {
    if(num > 31) return 'invalid day';
    if(num < 21) return days[num-1];
    if(num < 30) return days[20]   '-'   days[(num % 10)-1];
    if(num == 30) return days[21];
    return days[22]   '-'   days[(num % 10)-1];
    return str;
}

let date = new Date("01/31/2022");
let day = dayInWords(date.getDate());
let month = date.toLocaleString('default', { month: 'long' });
let year = yearInWords(date.getFullYear());
finalDateInWords = day   ' day of '   month   ' '   year;
console.log(finalDateInWords);

CodePudding user response:

Yep, that did it, Thanks!. Made a quick mod to the last bit so I could call it from my main script:

function englisDate(_date){
  let date = new Date(_date);
  let day = dayInWords(date.getDate());
  let month = date.toLocaleString('default', { month: 'long' });
  let year = yearInWords(date.getFullYear());
  finalDateInWords = day   ' day of '   month   ' '   year;
  console.log(finalDateInWords);
  return(finalDateInWords);
}
  •  Tags:  
  • Related