这个日期时间函数可以写得更有效吗?

发布于 2024-11-09 07:42:53 字数 1256 浏览 1 评论 0原文

以下函数返回两个日期时间值之间的差异(以字符串形式)。可以写得更高效/优雅吗?

/**
* @hint Returns the difference between two time strings in words.
*/
public string function timeAgoInWords(required date fromTime, date toTime=now())
{
    local.secondDiff = dateDiff("s", arguments.fromTime, arguments.toTime);

    if (local.secondDiff <= 60)
        return "#local.secondDiff# seconds ago";

    local.minuteDiff = dateDiff("n", arguments.fromTime, arguments.toTime);

    if (local.minuteDiff <= 60)
        if (local.minuteDiff < 2)
            return "1 minute ago";
        else return "#local.minuteDiff# minutes ago";

    if (local.minuteDiff <= 1440)
        if (local.minuteDiff <= 120)
            return "1 hour ago";
        else return "#int(local.minuteDiff/60)# hours ago";

    if (local.minuteDiff <= 2880)
        return "yesterday";

    if (local.minuteDiff <= 4320)
        return "2 days ago";

    local.monthDiff = dateDiff("m", arguments.fromTime, arguments.toTime);

    if (local.monthDiff <= 12)
        return "#dateFormat(arguments.fromTime, "mmm dd")# at #timeFormat(arguments.fromTime, "h:mm")#";

    return "#dateFormat(arguments.fromTime, "mmm dd 'yy")# at #timeFormat(arguments.fromTime, "h:mm")#";
}

The following function returns the difference between two date time values in words (as a string). Can it be written more efficiently/elegantly?

/**
* @hint Returns the difference between two time strings in words.
*/
public string function timeAgoInWords(required date fromTime, date toTime=now())
{
    local.secondDiff = dateDiff("s", arguments.fromTime, arguments.toTime);

    if (local.secondDiff <= 60)
        return "#local.secondDiff# seconds ago";

    local.minuteDiff = dateDiff("n", arguments.fromTime, arguments.toTime);

    if (local.minuteDiff <= 60)
        if (local.minuteDiff < 2)
            return "1 minute ago";
        else return "#local.minuteDiff# minutes ago";

    if (local.minuteDiff <= 1440)
        if (local.minuteDiff <= 120)
            return "1 hour ago";
        else return "#int(local.minuteDiff/60)# hours ago";

    if (local.minuteDiff <= 2880)
        return "yesterday";

    if (local.minuteDiff <= 4320)
        return "2 days ago";

    local.monthDiff = dateDiff("m", arguments.fromTime, arguments.toTime);

    if (local.monthDiff <= 12)
        return "#dateFormat(arguments.fromTime, "mmm dd")# at #timeFormat(arguments.fromTime, "h:mm")#";

    return "#dateFormat(arguments.fromTime, "mmm dd 'yy")# at #timeFormat(arguments.fromTime, "h:mm")#";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-11-16 07:42:53

这是我几个月前写的,基于上面评论中发布的 UDF Al Everett,并以 CF9 脚本风格编写。效率不会更高。事实上,它应该比您的实现慢,因为它多次调用 dateDiff(),并且需要预先设置 2 个数组,但总体行数更短且易于理解。

string function ago(required Date dateThen)
{
    var dateparts = ["yyyy","m","d","h","n"];
    var datepartNames = ["year","month","day","hour","minute"];
    var rightNow = Now();

    for (var i = 1; i <= 5; i++)    // 5 == arrayLen(dateparts)
    {
        var diff = dateDiff(variables.dateparts[i], dateThen, rightNow);

        if (diff > 1)
            return "#diff# #datepartNames[i]#s ago";

        if (diff == 1)
            return "#diff# #datepartNames[i]# ago";
    }

    return "Just Now";
}

This is what I wrote a few months ago, based on the UDF Al Everett posted above in the comment and written in CF9 script style. It won't be more efficient. In fact, it should be slower then your implementation 'cause it has multiple calls to dateDiff(), and needs to set up 2 arrays up front, but the overall line count is shorter and easily understandable.

string function ago(required Date dateThen)
{
    var dateparts = ["yyyy","m","d","h","n"];
    var datepartNames = ["year","month","day","hour","minute"];
    var rightNow = Now();

    for (var i = 1; i <= 5; i++)    // 5 == arrayLen(dateparts)
    {
        var diff = dateDiff(variables.dateparts[i], dateThen, rightNow);

        if (diff > 1)
            return "#diff# #datepartNames[i]#s ago";

        if (diff == 1)
            return "#diff# #datepartNames[i]# ago";
    }

    return "Just Now";
}
夏日浅笑〃 2024-11-16 07:42:53

我觉得不错。您可以使用第一个 diff (local.secondDiff) 进行所有测试,而不是重新比较,但这可能更容易阅读。

It looks good to me. You could instead use your first diff (local.secondDiff) for all your tests rather than re-diffing, but this is probably easier to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文