BST date string to javascript date

前端 未结 1 1225
野性不改
野性不改 2021-01-25 19:54

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

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

var data=\'Tue Apr         


        
相关标签:
1条回答
  • 2021-01-25 20:41
    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)
    
    0 讨论(0)
提交回复
热议问题