Livestamp插件Jquery-如何显示时间

Livestamp Plugin Jquery - how to show a time?

本文关键字:显示 时间 何显示 插件 Jquery- Livestamp      更新时间:2023-09-26

我使用这个jquery插件:livestamp。如果您知道,请告知如何仅显示当天的时间(小时分钟前)。第二天24小时后-显示标签"昨天"或简单的日期。非常感谢。

默认情况下,我认为livestamp无法做到这一点。

但是,在livestamp示例的底部,他们有一些代码可以在文本更改时通过挂接到change.livestamp事件来设置动画。

我们可以使用moment.js来修改此代码,以满足您的要求。

$('#animation').on('change.livestamp', function(event, from, to) {
    event.preventDefault(); // Stop the text from changing automatically
    // Get the original timestamp out of the event
    var originalTS = event.timeStamp;
    // Make a new moment object to compare the timestamp to
    var newDate = moment();
    // Use moment's .diff method to get the ms difference between timestamps
    var delta = newDate.diff(originalTS);
    // If the difference is less than a day's worth of ms
    if (delta < 86400000){
        // Use formatted text provided by the change event
        $(this).html(to);
    }
    else {
        // Format the moment object with whatever moment format you want
        $(this).html( newDate.format("dddd M/D/YYYY") );
    }
}).livestamp();

我还没有使用livestamp,但它的格式选项似乎依赖于现有的moment,所以这应该很有效。

Livestamp的源代码非常小,所以如果你有其他想做的事情,可以考虑自己破解它。