【问题标题】:Converting IST to local time in angular js在角度js中将IST转换为本地时间
【发布时间】:2018-05-11 18:00:14
【问题描述】:

从服务器我得到这种格式“21/11/2017 17:01:30”的时间,这次是在 IST 中。我想把这个时间转换成用户当地时间。

我正在使用下面给出的代码,

var startDateTimeArray = eachExeRun.startDate.split(" ");
var startDateArray = startDateTimeArray[0].split("/");
var startDate = new Date(startDateArray[2]+"-"+startDateArray[1]+"-"+startDateArray[0]+' '+startDateTimeArray[1]);

现在我的 startDate 是“2017 年 11 月 21 日星期二 16:59:29 GMT+0530(印度标准时间)”

在html中我是这样使用的

{{startDate | date:'yyyy-MM-dd HH:mm:ss'}}

在此之后为了测试我的代码,我将系统的时区更改为太平洋时区,然后我的 startTime 更改为“Tue Nov 21 2017 16:59:29 GMT-0800(Pacific Standard Time)” ,但在我看来仍然显示“2017-11-21 16:59:29”。

如何在不使用日期过滤器中的时区的情况下显示更新时间(日期:'yyyy-MM-dd HH:mm:ss Z')。

【问题讨论】:

  • 我建议你使用 moment.js,它非常适合日期时间转换,非常受欢迎的库
  • MomentJS 与 moment-timezone 可能会有所帮助。

标签: angularjs timezone datetime-conversion datefilter


【解决方案1】:

使用 moment.js 是最好的方式。

不过,您也可以编写一个自定义过滤器来转换日期。

试试这个:

    app.filter('myFormat', function() {
        return function(x) {
                var startDateTimeArray = x.split(" ");
                var startDateArray = startDateTimeArray[0].split("/");
                var startDate = new Date(startDateArray[2]+"-"+startDateArray[1]+"-"+startDateArray[0]+' '+startDateTimeArray[1]);
                console.log(startDate);
                //from timezone
                var IST_timezone_offset = -330; // IST timezone offset -always known
                var req_UTC_Milliseconds =  (IST_timezone_offset * 60000) ;
                //to timezone
                var local_timezone_offset = startDate.getTimezoneOffset(); 
                var local_UTC_Milliseconds = (local_timezone_offset * 60000);

                var final_date = new Date( startDate.getTime() + (req_UTC_Milliseconds - local_UTC_Milliseconds ) );

                return final_date;
            };
        });

Working Fiddle

【讨论】:

    【解决方案2】:

    使用 momentJs 非常简单:

    在下面找到工作代码sn-p:

    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script src="https://momentjs.com/downloads/moment.js"></script>
    <script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
    <script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>
    
    <body>
    
        <h2>My IST to Local System Date Convertor</h2>
        <div ng-app="myApp" ng-controller="dateCtrl">
            <p>{{convertIstToSystemLocalDateUsingTimeZone(IstStartDate)}}</p>
        </div>
    
        <script>
            var app = angular.module('myApp', []);
            app.controller('dateCtrl', function ($scope) {
                $scope.IstStartDate = '21/11/2017 17:01:30';
                $scope.convertIstToSystemLocalDateUsingTimeZone = function (incomingISTString) {
                    return moment.tz(incomingISTString, 'DD-MM-YYYY HH:mm:ss', 'Asia/Kolkata')
                        .local().format('DD-MM-YYYY HH:mm:ss');
                }
            });
        </script>
    
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 2012-12-25
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2015-03-10
      相关资源
      最近更新 更多