简体   繁体   中英

Mysql datetime string format as human readable date in angularjs?

I am retrieving date-time string from server and displaying to user in angularJs. I want to format date-time string in to human readable format. I prefer 10 sec ago type of format.

How it is possible in angularjs?

My Date String given below

"created_at": "2015-03-22 16:10:02",

I have tried with following function and filter but not working,

 $scope.convertToDate = function (stringDate) {
                    var dateOut = new Date(stringDate);
                    dateOut.setDate(dateOut.getDate() + 1);
                    return dateOut;
                };

filter,

{{convertToDate(action.created_at) | date:'medium'}}

In angularjs you could use 'filter' to achieve such a thing you want to do. Unfortunately angularjs does NOT have such a filter. But you can write your own filter in which you can use some 3rd party library including moment.js.

In js

angular.module('your-module', [])
.filter('yourfilter', function(){
    return function(input){
        //you can write your code. you can use some lib if you want.
    }
})

In html

{{action.created_at | yourfilter}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM