如何通过每次测量逐步显示日期?

时间:2014-10-09 19:20:07

标签: coldfusion coldfusion-10

我无法很清楚地表达我的头衔,但我想要做的是逐步显示两个日期之间的差异,直到可以使用最高测量单位。

这意味着我希望使用#DateDiff('s', mydate, Now())#最初以秒显示差异。 60秒后,它应显示分钟的差异。 60分钟后,我想让它一起显示小时和分钟的差异。然后在24小时后,应按天和小时显示差异。等等,直到数周,数月,数年都被覆盖。

我只能想象使用一个大<cfif>语句来做这件事,并且想知道在我开始编写这个东西之前是否有更好的方法。

1 个答案:

答案 0 :(得分:1)

<cfscript>
function CalcAge(Date1,Date2,depth) {
    param name="Arguments.depth" default="0";
    // Only alter local["partlist"] to list the dateparts you want to calculate.
    // Partlist args = cx = century, dx = decade, yyyy = year, m = month, ww = week, d = day, h = hour, n = minute, s = second
    // Partlist should also be in descending order.
    // Known Issue: The last element of partlist must be a coldfusion-recognized datepart (yyyy,m,ww,d,h,n,s)
    local["partlist"] = "cx,yyyy,m,ww,d,h,n,s";
    local["partWords"] = {cx="century,centuries",dx="decade,decades",yyyy="year,years",m="month,months",ww="week,weeks",d="day,days",h="hour,hours",n="minute,minutes",s="second,seconds"};

    local["fDates"] = {};
    // Decide which date is more recent, make the more recent as gDate
    if (DateCompare(Arguments["Date1"],Arguments["Date2"]) == -1) {
        // The First date is earlier than the second date
        local["fDates"]["lDate"] = Arguments["Date1"];
        local["fDates"]["gDate"] = Arguments["Date2"];
    } else if (DateCompare(Arguments["Date1"],Arguments["Date2"]) == 1) {
        // The second date is earlier than the first date, switch them
        local["fDates"]["lDate"] = Arguments["Date2"];
        local["fDates"]["gDate"] = Arguments["Date1"];
    } else {
        // The dates are equal, return and exit.
        return "0";
    }

    local["Difference"]=StructNew();
    local["pString"] = "";
    local["fDates"]["chDate"] = local["fDates"]["gDate"];
    local["cDepth"] = 0;
    while (DateDiff(listLast(local["partlist"]),local["fDates"]["lDate"],local["fDates"]["chDate"]) > 0) {
        for (i = 1; i <= listlen(local["partlist"]); i++) {
            ix = listGetAt(local["partlist"],i);
            param name="local.Difference.#ix#" default="0";

            if (ix == "cx") { // centuries
                while (DateDiff("yyyy",local["fDates"]["lDate"],local["fDates"]["chDate"]) >= 100) {
                    local["fDates"]["chDate"] = DateAdd("yyyy",-100,local["fDates"]["chDate"]);
                    local["Difference"][ix] += 1;
                }
            } else if (ix == "dx") { // decades
                while (DateDiff("yyyy",local["fDates"]["lDate"],local["fDates"]["chDate"]) > 10) {
                    local["fDates"]["chDate"] = DateAdd("yyyy",-10,local["fDates"]["chDate"]);
                    local["Difference"][ix] += 1;
                }
            } else {
                if (DateDiff(ix,local["fDates"]["lDate"],local["fDates"]["chDate"]) > 0) {
                    local["Changeby"] = DateDiff(ix,local["fDates"]["lDate"],local["fDates"]["chDate"]);
                    local["fDates"]["chDate"] = DateAdd(ix,0-Abs(local["changeby"]),local["fDates"]["chDate"]);
                    local["Difference"][ix] = local["changeby"];
                }
            }

            if (local["Difference"][ix] > 0) {
                local["cDepth"] = local["cDepth"] + 1;
                local["pString"] = listAppend(local["pString"],"#local["Difference"][ix]# #ListGetAt(local["partWords"][ix],min(2,local["Difference"][ix]))#",",");
                if (local["cDepth"] == Arguments["depth"]) {
                    return replace(local["pString"],",",", ","ALL");
                }
            }
        }
    }

    return replace(local["pString"],",",", ","ALL");
}

</cfscript>

并使用该功能:

<cfoutput>Adding 13 months. #CalcAge(Now(),DateAdd("m",13,Now()))#<br><br>
Brief: #CalcAge(Now(),DateAdd("m",13,Now()),2)#<br><br>

Adding 135 years. #CalcAge(DateAdd("yyyy",135,Now()),Now())#<br><br>
Brief: #CalcAge(Now(),DateAdd("yyyy",135,Now()),2)#<br><br>

Adding 73 hours. #CalcAge(Now(),DateAdd("h",73,Now()))#<br><br>
Brief: #CalcAge(Now(),DateAdd("h",73,Now()),2)#<br><br>

Depth: 100 (because none will have 100 elements); #CalcAge(Now(),DateAdd("s",1000000000,Now()),100)#<br><br>
Depth: 4; #CalcAge(Now(),DateAdd("s",1000000000,Now()),4)#<br><br>
Depth: 3; #CalcAge(Now(),DateAdd("s",1000000000,Now()),3)#<br><br>
Depth: 2; #CalcAge(Now(),DateAdd("s",1000000000,Now()),2)#<br><br>
Depth: 1; #CalcAge(Now(),DateAdd("s",1000000000,Now()),1)#<br><br>
Depth: 0 (depth 0 means unlimited); #CalcAge(Now(),DateAdd("s",1000000000,Now()),0)#<br><br></cfoutput>

这应该完全符合您的要求。

前两个参数是日期,应作为CalcAge(Date1,Date2)传递。

如果你将第三个参数“深度”设置为一个数字,它会将年龄向下舍入到所测量的最大单位(深度2表示'6天,4小时'而不是'6天,4小时, 3分28秒'。