Datejs获取日期之间的差异

时间:2012-06-26 21:57:24

标签: javascript

所以这是我的目标,我想知道多久以前某个日期是一个非常易读的字符串。类似于:'4天3小时前'。我正在使用datejs并且我的日期目前都是时代。

我知道我可以从另一个中减去一个纪元,取差异然后手动计算要输出的字符串。但这感觉就像datejs会处理的东西。但是我没有看到任何文件(任何js lib的文件)表示将日期对象或时期转换为一定时间的方法。

关于如何处理这个问题的任何想法?

更新

让我更清楚一点。当我说人类可读意味着

  • 1天前是'昨天'
  • 2天前是'2天前'
  • 30秒前是'刚才'
  • 1年零3天12秒前是'1年前'

我最终做了什么

    prettyAgo = (ago) ->
        ago = ago.getTime() if typeof ago is 'object'
        diff = ((new Date()).getTime() - ago) / 1000
        dayDiff = Math.floor(diff / 86400)
        return false if isNaN(dayDiff) or dayDiff < 0

        writer = ''

        if dayDiff is 0
            if diff < 1 then writer             = 'just now'
            else if diff < 60 then writer       = Math.floor(diff)+' seconds ago'
            else if diff < 120 then writer      = '1 minute ago'
            else if diff < 3600 then writer     = Math.floor(diff / 60)+' minutes ago'
            else if diff < 7200 then writer     = '1 hour ago'
            else if diff < 86400 then writer    = Math.floor(diff / 3600)+' hours ago'
        else if dayDiff is 1 then writer        = 'yesterday'
        else if dayDiff < 7 then writer         = dayDiff+' days ago'
        else if dayDiff is 7 then writer        = '1 week ago'
        else if dayDiff < 31 then writer        = Math.ceil( dayDiff / 7 )+' weeks ago'
        else writer                             = new Date(ago).toString 'MMM yyyy'

        return writer

2 个答案:

答案 0 :(得分:2)

DateJS通过包含一个名为TimeSpan的类(在time.js中定义,而不是在基本JS文件中)为您进行计算来处理此问题。它没有格式化,所以你需要这样的东西:

function showDiff(date1, date2) {
    var diff, rv, yrs;

    diff = new TimeSpan(date2 - date1);
    if (diff.days > 0) {
        // A day or more difference
        if (diff.days === 1) {
            rv = "yesterday";
        }
        else if (diff.days >= 365) {
            yrs = diff.days / 365;
            if (yrs === 1) {
                rv = "1 year ago";
            }
            else {
                rv = yrs + " years ago";
            }
        }
        else {
            rv = diff.days + " ago";
        }
    }
    else {
        // Less than 1 day difference
        if (diff.hours > 0) {
            if (diff.hours === 1) {
                rv = "1 hour ago";
            }
            else {
                rv = diff.hours + " hours ago";
            }
        }
        else if (diff.minutes > 0) {
            if (diff.minutes === 1) {
                rv = "1 minute ago";
            }
            else {
                rv = diff.minutes + " minutes ago";
            }
        }
        else {
            rv = "just now";
        }

        return rv;
    }

显然这并不完美,并没有处理闰年等等,但它应该给你这个想法。

答案 1 :(得分:1)

实际上你可以用纯JavaScript做到这一点。

function format(num) {
    var date = new Date(num),
        time = [];
    with(time) {
        push(date.getUTCDate() - 1);
        push(date.getUTCHours());
        push(date.getUTCMinutes());
        push(date.getUTCSeconds());
    }
    if (time[0] > 29) {
        time[0] = Math.floor(date / 86400000);
    }
    return time[0] + " day " + time[1] + " hours " + time[2] + " mintues " + time[3] + " seconds ago";
}

http://jsfiddle.net/DerekL/NqTnF/