PTN
PTN

Reputation: 1692

Convert from "yyyy-MM-ddTHH:mm:ss" to "yyyy-MM-dd" AngularJS

The problem I am having with is that since I am using ng-model, I cannot do something like {{ myDate | date: "yyyy-MM-dd" }}.

<div class="col-lg-7">
   <input type="datetime-local" class="form-control" ng-model="currentAccount.InceptionDate" ng-init="currentAccount.InceptionDate = parseDate(currentAccount.InceptionDate)">
</div>

I made a function to initialize the date in my controller

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

I put a break point at the return statement. It says that date is undefined. Another thing is that, InceptionDate when defined in my Account.cs class, its type is DateTime, not string. I'm not sure if this is the actual cause.

If my HTML looks like this, it works fine, but the date is not in the format I want (it is showing 2015-08-10T00:00:00):

<div class="col-lg-7">
   <input type="text" class="form-control" ng-model="currentAccount.InceptionDate" >
</div>

Upvotes: 1

Views: 1748

Answers (1)

Gillespie
Gillespie

Reputation: 6561

If currentAccount.InceptionDate is a both a Date object and a scope variable, you should be able to just do:

{{currentAccount.InceptionDate | date: "yyyy-MM-dd"}}

Example:

http://jsfiddle.net/rpgillespie/d65e5yfy/1/

Upvotes: 3

Related Questions