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

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

问题描述

由于从我收到的UTC时区的JSON响应。我需要将其转换为本地时间。

 <跨度类=TEXT-静音> {{trans.txnDate}}< / SPAN>

任何人都可以在此帮助???

谢谢..


解决方案

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

 <跨度类=TEXT-静音> {{trans.txnDate |日期:'YYYY-MM-DD HH:MM:SS Z'}}< / SPAN>

这将输出:

  2010-10-29 9时10分23秒0530

(假设 trans.txnDate = 1288323623006;

日期本文档在angularjs.org 。它是非常有帮助的不少例子!


编辑:

在回答您的评论,请使用以下来获得日期为 2014年10月17日

 <跨度类=TEXT-静音> {{trans.txnDate |日期:'DD MMM YYYY|小写}}< / SPAN>

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

EDIT2:

在回答您的其他评论,请使用以下code。问题是,你所得到的字符串的格式不正确,让日期对象无法识别它。我已经在控制器格式化,然后传递给视图。

\r
\r

函数MyCtrl($范围){\r
  VAR dateString =2014:10:17T18:30:00Z\r
  dateString = dateString.replace(/:/,' - '); //取代第一:字符\r
  dateString = dateString.replace(/:/,' - '); //取代第二:字符\r
  $ scope.date =新的日期(dateString);\r
}

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/angularjs/1.2.23/angular.min.js\"></script>\r
\r
&LT; D​​IV NG-NG应用程序控制器=MyCtrl&GT;\r
  {{日期|日期:'DD MMM YYYY|小写}}\r
&LT; / DIV&GT;

\r

\r
\r

更换的JS code能找到一个更聪明的办法来替代第2次出现的改善字符

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???

Thanks..

解决方案

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.

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

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