javascript - 丹麦日期到新 DATE()

标签 javascript jquery

于2014年1月1日上午10:00

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

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

上面的行给了我“无效日期”。

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

最佳答案

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

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
}

关于javascript - 丹麦日期到新 DATE(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25905287/

相关文章:

直到将鼠标悬停在 ie7 上才显示 jquery 对话框按钮

javascript - jQuery 砌体堆叠

javascript - visjs.org 库 : display only event dates

javascript - 如何以时间间隔循环列表和淡入?

javascript - 如何禁用 "href"属性的默认 <a> 行为?

javascript - 为什么读不到 '.appendChild'?页面怎么可能是未定义的?

javascript - 带有“确定”和“取消”按钮的 jquery 对话框

javascript - 使用用户输入搜索数组

javascript - public_html 中的两个索引文件

javascript - 在 View 中激活 javascript 动画