本文介绍了Livestamp插件Jquery - 如何显示时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个jquery插件:。
如果您知道,请告诉我们,如何在当天显示时间(小时分钟以前)。第二天24小时后 - 显示标签昨天或简单日期
谢谢!

I use this jquery plugin: livestamp.If you know, tell please, how to show time (hours, minutes ago) only for the current day. After 24 hours on next day - to show label "yesterday" or simple date.Thank you!

推荐答案

默认情况下,我认为livestamp不能这样做。

By default, I don't think livestamp can do this.

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

But, at the bottom of livestamp's examples, they have some code to animate the text when it changes by hooking into the change.livestamp event.

我们可以使用来修改这个代码来做你想要的。

We can use moment.js to modify this code to do what you're asking.

$('#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,但它似乎依赖于格式化选项存在的时刻,所以这应该可行。

I haven't used livestamp, but it seems to rely on moment existing for its formatting options, so this should just work.

Livestamp的来源是超级小,所以如果你有其他想要的东西,请考虑自己进行黑客攻击。

Livestamp's source is super small, so consider hacking on it yourself if you have other stuff you want to be able to do.

这篇关于Livestamp插件Jquery - 如何显示时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!