Angular filter that converts date and time into one formatted string

I use data from an API that returns date and time in two different key / value pairs (date and time).

<!DOCTYPE html>
<html ng-app="app">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="ctrl">

  Date {{date}} - Time {{time}}
  <br/>
  {{date  | dateformatter}}

</body>
</html>
angular.module("app",[]).controller("ctrl", function($scope) {
  $scope.date = "03/13/2014";
  $scope.time = "8:10:56";

}).filter("dateformatter", function($filter){
  // this should return 'yyyy-MM-dd h:mm:ss'
  return function(dt) {
    return "2014 03 13 8:10:56";
  }
})

Can I use a filter to convert it to a single formatted string?

Here is an example in jsBin

+4
source share
3 answers

I have transformed the facilities Dateand Timein Dateand used a

So, the controller looks like this:

app.controller("ctrl", function($scope) {
  $scope.date = "03/13/2014";
  $scope.time = "8:10:56";

  $scope.newDate = new Date( $scope.date + ' ,' + $scope.time).getTime();     
});

and HTML:

{{newDate  | date: 'yyyy-MM-dd h:mm:ss'}}

Demo Fiddle

+4
source

Another quick way to do this:

angular
.module('PrivateModule')
.controller('MyController', ['$scope', function ($scope) {

    $scope.Date = function(date) {
        return new Date(date);
    }

}

Then, in your opinion:

<span>{{Date(obj.start) | date : 'dd/MM/yyyy'}}</span>
+1
source

:

+(function(angular, undefined) {
  angular
    .module('app')
    .filter('timestamp', filter);

  function filter() {
    return function filterFn(input) {
      return ( Date.parse(input) );
    }
  }
})(angular);

: {{ date | timestamp | date: 'MMM d, yyyy' }}. {{ date + ' ,' + time | timestamp | date: 'MMM d, yyyy' }}.

, , .

, , - , gist/snippet (, ).

0
source

Source: https://habr.com/ru/post/1535910/


All Articles