如何将 Moment.js 与 ui.grid 一起使用



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

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

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

不幸的是,这返回:

类型错误: 时刻(...(.倒计时不是一个函数

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

更新

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("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'

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

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

您可以先准备日期源数组,然后分配给 UI 网格

相关内容

最新更新