Convert date into word format

后端 未结 5 1630
醉话见心
醉话见心 2021-01-28 05:42

I\'m not sure how i will do it, I want convert date in english word format, like this if date is 10-10-1988 then

In English- tenth October nineteen eighty eight
         


        
5条回答
  •  面向向阳花
    2021-01-28 06:15

    You could try something like this:

    var dateTime = new Date($("#selector").datepicker("getDate"));
    var month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var date = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth', 'twentieth', 'twenty-first', 'twenty-second', 'twenty-third', 'twenty-fourth', 'twenty-fifth', 'twenty-sixth', 'twenty-seventh', 'twenty-eighth', 'twenty-ninth', 'thirtieth', 'thirty-first'];
    var strDateTime =  "Day " + date[dateTime.getDate()-1] + " of " + month[dateTime.getMonth()] + " in the year " +  toWords(dateTime.getFullYear());
    console.log(strDateTime);
    
    function toWords(s){
        var th = ['','thousand','million', 'billion','trillion'];
        var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
        var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
        var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
        s = s.toString();
        s = s.replace(/[\, ]/g,'');
        if (s != parseFloat(s)) {
            return 'not a number';
        }
        var x = s.indexOf('.');
        if (x == -1) x = s.length;
        if (x > 15) return 'too big';
        var n = s.split('');
        var str = '';
        var sk = 0;
        for (var i=0; i < x; i++) {
            if ((x-i)%3==2) {
                if (n[i] == '1') {
                    str += tn[Number(n[i+1])] + ' ';
                    i++;
                    sk=1;
                } else if (n[i]!=0) {
                    str += tw[n[i]-2] + ' ';
                    sk=1;
                }
            } else if (n[i]!=0) {
                str += dg[n[i]] +' ';
                if ((x-i)%3==0) str += 'hundred ';
                sk=1;
            }
            if ((x-i)%3==1) {
                if (sk) str += th[(x-i-1)/3] + ' ';
                sk=0;
            }
        }
        if (x != s.length) {
            var y = s.length;
            str += 'point ';
            for (var i=x+1;    i

提交回复
热议问题