Javascript 丹麦日期到新日期()
ons okt 01 2014上午10:00 这是2014年10月1日星期三上午10:00(丹麦语)Javascript 丹麦日期到新日期(),javascript,jquery,Javascript,Jquery,ons okt 01 2014上午10:00 这是2014年10月1日星期三上午10:00(丹麦语) var dt = new Date("ons okt 01 2014 10:00 AM"); 上面的一行给了我“无效日期” 如何在dt变量中获得正确的日期?由于格式本身对日期有效,因此可以正确解析字符串,您应该实现一个函数,将星期和月份的日期从丹麦语转换为英语 function fromDanish(dateString) { //A table whose properties ar
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
}
由于格式本身对Date有效,因此可以正确解析字符串,因此应该实现一个函数,将周和月的日期从丹麦语转换为英语
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
}
由于格式本身对Date有效,因此可以正确解析字符串,因此应该实现一个函数,将周和月的日期从丹麦语转换为英语
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
}
由于格式本身对Date有效,因此可以正确解析字符串,因此应该实现一个函数,将周和月的日期从丹麦语转换为英语
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
}
我建议使用日期库:
.toLowerCase()
,因为日期可能包含大写字母。谢谢Claudix。我正在寻找一个通用的解决方案。丹麦字符串只是一个例子。我正在寻找除英语字符串以外的任何字符串。这个答案可以,但在拆分日期字符串之前,我会包含一个.toLowerCase()
,因为日期可能包含大写字母。谢谢Claudix。我正在寻找一个通用的解决方案。丹麦字符串只是一个例子。我正在寻找除英语字符串以外的任何字符串。这个答案可以,但在拆分日期字符串之前,我会包含一个.toLowerCase()
,因为日期可能包含大写字母。谢谢Claudix。我正在寻找一个通用的解决方案。丹麦字符串只是一个例子。我正在寻找除英语字符串以外的任何字符串。这个答案可以,但在拆分日期字符串之前,我会包含一个.toLowerCase()
,因为日期可能包含大写字母。