如何将BST日期字符串转换为Javascript日期对象?
以下代码给出了BST的错误,但适用于其他时区
var data='Tue Apr 28 16:15:22 BST 2015';
var date = new Date(data);
console.log(date)
输出
Invalid Date
var data='Tue Apr 28 16:15:22 BST 2015';
var date = new Date(data);
console.log(date)
var data2='Fri Mar 27 11:53:50 GMT 2015'
var date2 = new Date(data2);
console.log(date2)
答案 0 :(得分:2)
将这些放在一起
var timezone_map = {
'BST': 'GMT+0100'
};
function re_order(str) {
var re = /^(\w+) (\w+) (\d\d) (\d\d:\d\d:\d\d) (\w+) (\d\d\d\d)$/;
return str.replace(re, function ($0, day, month, date, time, zone, year) {
return day + ' ' + month + ' ' + date + ' ' + year + ' ' + time + ' ' + (timezone_map[zone] || zone);
});
}
new Date(re_order('Tue Apr 28 16:15:22 BST 2015'));
// Tue Apr 28 2015 16:15:22 GMT+0100 (GMT Daylight Time)