将日期转换为word格式

时间:2014-03-20 07:06:21

标签: javascript jquery html css jquery-ui

我不知道我会怎么做,我想用英文单词格式转换日期, 如果日期是10-10-1988那么

In English- tenth October nineteen eighty eight

请帮帮我

小提琴Here

5 个答案:

答案 0 :(得分:1)

像这样的东西

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])

答案 1 :(得分:1)

对于About.com上的文章,我找到了一个完美的解决方案。

上面的参考资料帮助我解析了数字中的数字,而我稍微调整了几个月来帮助我。

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
}

请按照此Fiddle获取完整代码。希望这有助于某人。

答案 2 :(得分:0)

使用Date JS或者您也可以使用Jquery UI日期选择器。

Jquery UI Date Picker

答案 3 :(得分:0)

您可以参考角度应用的dobToWords角度库来帮助您将日期对象转换为单词。

例如: 对于 21-4-1990 日期,它会将日期转换为单词,并将输出为“二十一月四月九十九”

    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);
     }]);

答案 4 :(得分:0)

您可以尝试这样的事情:

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,' ');
}