于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/