Reputation: 301
i have problem in angularjs when i use <input type="date" ng-model="trip.start">
i think data in trip.start
is 2017-5-10
but data in trip.start
is 2017-5-10T17:00:00.000Z
How to parse trip.start
just yyyy-mm-dd
without time.
please helpme if you have a idea to parse it.
Upvotes: 0
Views: 1429
Reputation: 663
I will suggest to use moment for date format. After getting data from server use:
$scope.trip.start = moment($scope.trip.start).format("YYYY-MM-DD");
For this to work, include Moment.js library.
Upvotes: 1
Reputation: 8709
Your date value seems to be a JavaScript date. Angular already provides a filter for formatted date outputs:
{{trip.start | date:'yyyy-MM-dd'}}
Alternatively you could convert the date to string:
function formatDate(date) {
var date = date || new Date();
var today = new Date(date);
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
return (yyyy + '-' + mm + '-' + dd);
}
Upvotes: 0
Reputation: 1
I don't know how to completely parse it, but try date filter when you are using the data. Something like this syntax:
{{ date_expression | date : format : timezone}}
in your case:
{{trip.start | date: 'yyyy-MM-dd'}}
For more reference read the documentation here
Upvotes: 0
Reputation: 403
The ng-model data must be a Date Object.
Then your input box will show the date. If you want to show formatted date then you can do something like this-
$scope.formattedDate = $filter('date')($scope.trip.start, "your date format");
Add $filter as a dependency in your controller and bind $scope.formattedDate to your input box.
Upvotes: 1
Reputation: 2602
You can do,
var date = trip.start.split("T")[0]
you will get expected result.
Upvotes: 0