简体   繁体   English

BST日期字符串到javascript日期

[英]BST date string to javascript date

How can I convert the BST date string to Javascript date object? 如何将BST日期字符串转换为Javascript日期对象?

The follwing code gives me error for BST but works for other timezone 以下代码给我BST错误,但在其他时区有效

var data='Tue Apr 28 16:15:22 BST 2015';
var date = new Date(data);
console.log(date)

Output 产量

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) 

  1. Don't rely on Date to know timezone names 不要依靠日期知道时区名称
  2. Don't mix a date with a time ; 不要把日期时间混为一谈 ; the year should be before the time, the timezone should be the very last thing 年份应该在时间之前,时区应该是最后一件事

Putting these together 将它们放在一起

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM