Mateusz Kibiłko
Mateusz Kibiłko

Reputation: 62

datetime format in angularjs

In AngularJs I get a date in this format : ["2018-01-03T09:30:54.264Z"]. I want it to be in the format : "2018-01-03T09:30:00" How can I get it in this format?

Controller.js

var myDate = new Date();
$scope.form.date.push(myDate);

template

<div class="col-lg-12 photo-container" ng-repeat="photo in selectedPhotos track by $index" ng-if="add">
            <input id="myDate" type="datetime-local" name="date[]" ng-model="form.date[$index]">
            <img ng-src="{{photo}}" class="thumb">
        </div>

Upvotes: 0

Views: 2661

Answers (3)

Aref Zamani
Aref Zamani

Reputation: 2062

first inject $filter to your controller then try this:

var myDate = new Date();
myDate = $filter('date')(myDate , "YYYY-MM-DDThh:mm"); 
$scope.form.date.push(myDate);

Upvotes: 0

Aleksey Solovey
Aleksey Solovey

Reputation: 4191

You are not being clear about how you are doing it, but here is a working example (work from it):

var app = angular.module('myApp', []);
app.controller('dateCtrl', function($scope) {

  $scope.form = {
    "date": []
  };
  for (let i = 0; i < 5; i++) {
    var myDate = new Date();
    $scope.form.date.push(myDate);
  }

});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body>

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

    <div ng-repeat="date_ in form.date track by $index">
      <div>1: {{ date_ | date : 'yyyy-MM-ddThh-mm-ss' }}</div>
      <div>2: {{ form.date[$index] | date : 'yyyy-MM-ddThh-mm-ss' }}</div>
      <br/>
    </div>

  </div>
</body>

</html>

Upvotes: 1

Manoj Kumar Singh
Manoj Kumar Singh

Reputation: 34

you can use moment.js to achieve that i.e.

var d = moment("2018-01-03T09:30:54.264Z").format('YYYY-MM-DDThh:mm:ss') . 

it will give you the required format, put whatever format you want as string in format() function.

Upvotes: 0

Related Questions