Search code examples
javascriptangularjsangular-ui-gridui-grid

How to use Moment.js with ui.grid


I am trying to convert all the dates rendered on Angularjs UI grid with countdowns. The dates are all in this format 01/19/2018 21:30 (Short US), Instead I want it to be xx Hour xx minutes left.

I have been trying with JavaScript and custom cell classes but I can't get it to work. I never worked with moment.js before but it seems that it might solve my issue, any ideas how I can run it inside of ui.grid?
this is what I have in columnDefs

{ field: 'DueDate', cellFilter: 'date:"short"'}

Edit : Based on the answers bellow I am trying to alter the dates as soon as they come from hhtpget and replace them by countdowns

$http.get("xxx/json").then(function(response) { response.data.forEach( function(row, index) { row.MakerDueDate = moment("row.MakerDueDate").countdown().toString(); }); $scope.gridOptions.data = response.data; }); 

Unfortunately this returns :

TypeError: moment(...).countdown is not a function

I included moment.js, countdown.js and moment-countdown.min.js in my html


Solution

  • Updated:

    moment().countdown(new Date('01/19/2018 21:30'), countdown.HOURS|countdown.MINUTES, NaN, 2).toString();
    

    output

    "168 hours and 29.34 minutes"

    I run this code from the console... I include all 3 js

    <script src="moment.min.js"></script>
    <script src="countdown.min.js"></script>
    <script src="moment-countdown.min.js"></script>
    

    maybe you can try this moment-countdown examples :

        moment("1982-05-25").countdown().toString(); //=> '30 years, 10 months, 14 days, 1 hour, 8 minutes, and 14 seconds'
    
        //accepts a moment, JS Date, ISO-8601 string, or any other single arg taken my Moment's constructor
        moment("1955-08-21").countdown("1982-05-25").toString(); //=> '26 years, 9 months, and 4 days'
    
    
    moment().countdown("1982-05-25", countdown.MONTHS|countdown.WEEKS, NaN, 2).toString(); //=> '370 months, and 2.01 weeks'
    
    
        //also works with the args flipped, like diff()
        moment("1982-05-25").countdown("1955-08-21").toString(); //=> '26 years, 9 months, and 4 days'