0

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>
5
  • 1
    You mean display it in that format or being handled in that format? Commented Jan 3, 2018 at 9:38
  • 1
    Possible duplicate of How to format a JavaScript date Commented Jan 3, 2018 at 9:39
  • <div>{{ form.date[$index] | date : 'yyyy-MM-ddThh-mm-ss' }}</div> Commented Jan 3, 2018 at 9:40
  • Yes, display it in that format. And the date must be an object Commented Jan 3, 2018 at 9:40
  • @AlekseySolovey this doesn't work for me Commented Jan 3, 2018 at 9:42

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

How can i get date with format : '2017-12-31T11:12:00.000Z' ? I try this: var d = moment(myDate).format('YYYY-MM-DDThh:mm:ss.000Z'); But this doesnt work
Ok, doesnt matter. I did to do what I wanted
just use moment().toISOString() you will get ISO format of the date(i.e.-2017-12-31T11:12:00.000Z)
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.