如何在ui.grid

时间:2018-01-26 13:57:21

标签: javascript angularjs angular-ui-grid ui-grid

我正在尝试使用倒计时转换在Angularjs UI网格上呈现的所有日期。日期都是这种格式01/19/2018 21:30(美国短线),相反我希望它是xx小时xx分钟左右。

我一直在尝试使用JavaScript和自定义单元格类,但我无法让它工作。我之前从未使用过moment.js,但它似乎可以解决我的问题,我可以在ui.grid中运行它的任何想法吗?
这就是我在columnDefs

中的含义
{ field: 'DueDate', cellFilter: 'date:"short"'}

编辑: 根据下面的答案我试图改变日期,一旦它们来自hhtpget并用倒计时替换它们

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

不幸的是,这会返回:

  

TypeError:moment(...)。countdown不是函数

我在html中包含了moment.js,countdown.js和moment-countdown.min.js

2 个答案:

答案 0 :(得分:0)

<强>更新

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

输出

  

“168小时29.34分钟”

我从控制台运行此代码... 我包括所有3个js

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

也许你可以尝试这个moment-countdown 例子:

    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'

答案 1 :(得分:0)

要使用时刻js仅显示xx小时xx分钟,语法将为:

const formattedDate = moment(date).format('hh:mm');

您可以先准备日期源数组,然后分配到您的ui网格