丹麦日期到新的DATE()

时间:2014-09-18 06:05:36

标签: javascript jquery

ons okt 01 2014 10:00 AM

这是丹麦语的 wed oct 01 2014 10:00 AM

var dt = new Date("ons okt 01 2014 10:00 AM");

以上行给了我"无效日期"。

如何在dt变量中获取正确的日期?

2 个答案:

答案 0 :(得分:2)

由于格式本身对于日期正确解析字符串是有效的,因此您应该实现一个将丹麦语言中的日期和月份翻译成英语的函数。

function fromDanish(dateString) {
    //A table whose properties are Danish names for days of week and whose values are English names.
    var daysOfWeek = {'son': 'sun', 'man': 'mon', 'tir': 'tue', 'ons': 'wed', 'tor': 'thu', 'fre': 'fri', 'lor': 'sat'};

    //A table whose properties are Danish names of months and whose values are English names.
    var months = {'jan': 'jan', 'feb': 'feb', 'mar': 'mar', 'apr': 'apr', 'maj': 'may', 'jun': 'jun', 'jul': 'jul', 'aug': 'aug', 'sep': 'sep', 'okt': 'oct', 'nov': 'nov', 'dec': 'dec'};


    var tokens = dateString.split(' '); //Split the Danish string by spaces
    var dow = tokens[0]; //This is the Danish day of week
    var month = tokens[1]; // and this is the Danish month 

    tokens[0] = daysOfWeek[dow]; //Get the English day of week from the table
    tokens[1] = months[month]; //Get the English month from the table
    var english_date = tokens.join(' '); //Join all tokens again
    return new Date(english_date); //Now Date() constructor knows how to parse the date string
}

答案 1 :(得分:1)

我建议使用日期库: