0

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.

3
  • It will get the data like that, you need to trim in controller after getting data Commented May 11, 2017 at 5:13
  • you have some example to trim in controller after getting data Commented May 11, 2017 at 5:44
  • I am not exactly having any list of tutorials, but you may use $scope.trip.start.getFullYear() to get year and simillarly .getMonth()and .getDate() to get month and date and in last concatenate futher for full date. OR you may use some filters available with angularjs to get desired results. Commented May 11, 2017 at 5:50

5 Answers 5

1

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.

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

6 Comments

$trip.start eh? ;-)
i have error when i use your code, my input can't show
can you check console for error and post that over here? And yes do you include moment in your page?
@RizkaNugroho you gotta include and inject momentJS library dependency (angularMoment?).. Answer can be updated with that information
yeah i don't have install angularmoment, and error in console is moment is not defined
|
1
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.

Comments

0

You can do,

var date = trip.start.split("T")[0]

you will get expected result.

Comments

0

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

Comments

0

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

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.