Convert date into word format

后端 未结 5 1620
醉话见心
醉话见心 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:09

    Use Date JS or also you can use Jquery UI date picker.

    Jquery UI Date Picker

    0 讨论(0)
  • 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<y; i++) str += dg[n[i]] +' ';
        }
        return str.replace(/\s+/g,' ');
    }
    
    0 讨论(0)
  • 2021-01-28 06:19

    You can refer dobToWords angular library for angular application which can help you out in converting the date object into the words.

    For Example: For date 21-4-1990 it will convert the date into words and will give you output as "Twenty-First April Nineteen Ninety"

        angular.module('sampleApp',[dobToWords])
       .controller('SampleCtrl',['$scope','convertDobToWords',function($scope,convertDobToWords){
            var dateOfBirth = new Date('04/21/1990');
            var dobIntoWords = convertDobToWords.convertIntoWords(dateOfBirth);
            console.log('Date of Birth in Words : '+ dobIntoWords);
         }]);
    
    0 讨论(0)
  • 2021-01-28 06:27

    Something like that

    var wDays = ['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 wMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    var wNumbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone']
    
    var date = new Date()
    var day = parseInt(date.getUTCDate()) - 1;
    var month = parseInt(date.getUTCMonth());
    var year = date.getUTCFullYear().toString();
    
    var x = year.charAt(0)
    var xx = year.charAt(1)
    var xxx = year.charAt(2)
    var xxxx = year.charAt(3)
    
    
    var a = parseInt(x + xx) - 1
    var b = parseInt(xxx) - 1
    var c = parseInt(xxxx) - 1
    console.log(wDays[day] + ' ' + wMonths[month] + ' ' + wNumbers[a] + ' ' + wNumbers[b] + ' ' + wNumbers[c])

    0 讨论(0)
  • 2021-01-28 06:28

    With credits to an article on About.com, I figured out a perfect solution to this question.

    The above reference helped me parse the Numerals in date to Words, while I tweaked it a little for helping me with Months.

    var stu = $("#txtStartDate").val().split('-');
    var wMonths=['january','february','march','april','may','june','july','august','september','october','november','december']
    var final  = toWords(stu[0]) + " " + wMonths[parseInt(stu[1])-1] + " " + toWords(stu[2])   
    alert(final);
    
    function toWords(s)
    {
        // Open Fiddle for complete code
        // convert number to words
    }
    

    Follow this Fiddle for the complete code. Hope this helps someone.

    0 讨论(0)
提交回复
热议问题