如何使用时刻将时区字符串转换为时区偏移量?

时间:2016-05-29 05:11:35

标签: angularjs momentjs

我为每个用户保存了时区,以及#34;欧洲/布加勒斯特"或"欧洲/纽约"。

然而,角度只接受时区偏移字符串:" + 0400"或" -0330"将值作为过滤器传递给日期时。

如何将这些字符串转换为时区偏移字符串?

1 个答案:

答案 0 :(得分:2)

假设您已安装moment-timezone库:

(function ()
{
    var app = angular.module("app", []);

    function HomeController()
    {
        var vm = this;
        vm.europe = moment().tz('Europe/Bucharest').format("ZZ");
        vm.us = moment().tz('America/New_York').format("ZZ");
    }

    app.controller("HomeController", [HomeController]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.4/moment-timezone-with-data-2010-2020.min.js"></script>

<!DOCTYPE html>
<html lang="en" ng-app="app">
    <head>
        <meta charset="UTF-8">
        <title>Angular JS App</title>        
    </head>
    <body>

         <div class="container" ng-controller="HomeController as homeCtrl">
            <h4>Europe: {{homeCtrl.europe}}</h4>
            <h4>America: {{homeCtrl.us}}</h4>
        </div>


    </body>
</html>