BST日期字符串到javascript日期 [英] BST date string to javascript date

查看:78
本文介绍了BST日期字符串到javascript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将BST日期字符串转换为Javascript日期对象?

How can I convert the BST date string to Javascript date object?

以下代码给我带来了BST错误,但可用于其他时区

The follwing code gives me error for BST but works for other timezone

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)

推荐答案


  1. 不要依赖 Date 来了解时区名称

  2. 不要混用 date 时间;年份应该早于时间,时区应该是最后一件事

  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

将它们放在一起

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)

这篇关于BST日期字符串到javascript日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆