繁体   English   中英

如何使用javascript / Angular.js反转时间戳

[英]How to reverse time stamp using javascript/Angular.js

我需要一个帮助,我需要使用Angular.js / Javascript以相反的格式显示现有时间戳。 我在下面解释我的代码。

$scope.timestamp=2016-12-16 07:02:15 am

我有上面的时间戳,但是我需要反转上面的值,如下。

$scope.originalStamp=16/12/2016 07:02:15 am

所以我需要转换上面的格式。 请帮我。

您正在使用角度。 所以请尝试使用$filter

   $scope.timestamp =  '2016-12-16 07:02:15 am'
   $scope.originalStamp = $filter('date')
                          (new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');

更新的演示

这是完整的代码

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="datCtrl">

<p> {{originalStamp}} </p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope,$filter) {

   $scope.timestamp =  '2016-12-16 07:02:15 am'
  $scope.originalStamp = $filter('date')(new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');

});
</script>

<p>The date filter formats a date object to a readable format.</p>

</body>
</html>

为什么不从原始时间戳格式化日期。 您可以随意设置格式。

var date= new Date(timestamp);

var formatDate = function(date){
    return date.getDate() + "/" + date.getMonth() + "/" +date.getYear() + " "+  date.getHours() + ":" + date.getMinutes() + ":" + date.getMintutes() + ":" + date.getSeconds();
}

$scope.myDate = formatDate(date);

ng-bind="usercart.date|date:'yyyy-MM-dd HH:mm:ss'" ,您可以在此处定义所需的日期格式。...

添加此代码ng-bind="usercart.date|date:'dd-MM-yyyy HH:mm:ss'"

输出:

Last Updated Cart 05-01-2017 14:58:38

干得好

 var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; $scope.date='2016-12-16 07:02:15 am'; $scope.req = function(date){ var dateOut = new Date(date); return dateOut; }; }); 
 <!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script>document.write('<base href="' + document.location + '" />');</script> <link rel="stylesheet" href="style.css" /> <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> <script src="app.js"></script> </head> <body ng-controller="MainCtrl"> <p ng-bind="req(date) | date:'dd/MM/yyyy hh:mm:ss a'"></p> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM