使用 angularjs 将 UTC 转换为本地时间 [英] convert UTC to local time using angularjs

查看:33
本文介绍了使用 angularjs 将 UTC 转换为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为来自 json 的响应,我得到了 UTC 时区.我需要将其转换为当地时间.

<span class="text-muted">{{trans.txnDate}}</span>

有人可以帮忙吗?

解决方案

EDIT (2nd Jan 2017): 请参考@Jason 的回答,它比这个更好,因为它使用自定义过滤器来修复日期格式 -这是更 Angular 的方式.


我的原始答案和

您可以使用 date 过滤器来格式化日期:

{{trans.txnDate |日期:'yyyy-MM-dd HH:mm:ss Z' }}</span>

这将输出:

2010-10-29 09:10:23 +0530

(假设 trans.txnDate = 1288323623006;)

查看 angularjs.org 中 date 的文档.它有很多非常有用的例子!


为了回应您的评论,请使用以下内容获取日期为 2014 年 10 月 17 日:

{{trans.txnDate |日期:'dd MMM yyyy' |小写}}</span>

检查我上面提到的文档链接.

为了回应您的其他评论,请使用以下代码.问题是您获取的字符串格式不正确,因此 Date 对象无法识别它.我已经在控制器中对其进行了格式化,然后传递给了视图.

function MyCtrl($scope) {var dateString = "2014:10:17T18:30:00Z";dateString = dateString.replace(/:/, '-');//替换第一个 ":" 字符dateString = dateString.replace(/:/, '-');//替换第二个:"字符$scope.date = new Date(dateString);}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><div ng-app ng-controller="MyCtrl">{{日期 |日期:'dd MMM yyyy' |小写 }}

可以通过找到一种更智能的方法来替换 : 字符的前 2 次出现来改进用于替换的 JS 代码.

As a response from the json I am getting the UTC timezone. I need to convert it to local time.

<span class="text-muted">{{trans.txnDate}}</span>

Can anyone help on this?

解决方案

EDIT (2nd Jan 2017): Please refer @Jason's answer, it is better than this one since it uses custom filter to fix the date format - that's the more Angular way of doing it.


My original answer and edits:

You could use the date filter to format the date:

<span class="text-muted">{{trans.txnDate | date:'yyyy-MM-dd HH:mm:ss Z' }}</span>

This will output:

2010-10-29 09:10:23 +0530

(assuming trans.txnDate = 1288323623006;)

See this documentation of date in angularjs.org. It has quite a few examples that are very helpful!


EDIT:

In response to your comment, use the following to get the date as 17 oct 2014:

<span class="text-muted">{{trans.txnDate | date:'dd MMM yyyy' | lowercase }}</span>

Check the documentation link that I mentioned above.

EDIT2:

In response to your other comment, use the following code. The problem is that the string that you are getting is not properly formatted so the Date object is not able to recognise it. I have formatted it in the controller and then passed to the view.

function MyCtrl($scope) {
  var dateString = "2014:10:17T18:30:00Z";
  dateString = dateString.replace(/:/, '-'); // replaces first ":" character
  dateString = dateString.replace(/:/, '-'); // replaces second ":" character
  $scope.date = new Date(dateString);
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app ng-controller="MyCtrl">
  {{date | date:'dd MMM yyyy' | lowercase }}
</div>

The JS code for replacement can be improved by finding a smarter way to replace the first 2 occurrences of : character.

这篇关于使用 angularjs 将 UTC 转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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