自定义功能的日期时间增强?

时间:2014-07-19 16:01:52

标签: coldfusion

我目前正在修改Ray的现有功能,该功能会检查已用时间和剩余时间:

这是功能:

function ago(date){
    var interval    = "";
    var offset        = 0;
    var result        = 0;
    if (isDate(arguments.date)){
        var formattedDate = dateFormat(arguments.date, "dddd dd mmmm yyyy");

        var k = datediff('d',arguments.date,now());
        //writedump(k);
        //abort;

        if(k contains '-') {
            if (dateDiff("s", now(), arguments.date) < 60){
                // less than 1 minute show interval in seconds
                offset    = dateDiff("s", now(),arguments.date);
                interval= offset == 1 ? "second":"seconds";
                result    = "#offset# #interval# left";
            }else if (dateDiff("n",now(),arguments.date) < 60){
                // less than 1 hour show interval in minutes
                offset    = dateDiff("n", now(),arguments.date);
                interval= offset == 1 ? "minute":"minutes";
                result    = "#offset# #interval# left";

            }else if (dateDiff("h",now(),arguments.date) < 24){
                // less than 24 hours display interval in hours
                offset    = dateDiff("h", now(), arguments.date);
                interval = offset == 1 ? "hour":"hours";
                result    = "#offset# #interval# left";

            }else if (dateDiff("d",now(),arguments.date) < 2){
                // less than 2 days display yesterday
                result    = "Tommarrow";
            }else if (dateDiff("d", now(), arguments.date) < 7){
                // less than 7 days display day
                result    = dayOfWeekAsString( dayOfWeek( arguments.date ));
            }else if (dateDiff("w", now(), arguments.date)){
                offset    = dateDiff("w",now(), arguments.date);
                interval = offset == 1 ? "week":"weeks";
                result    = "#offset# #interval# left";
            }else if (dateDiff("m", now(), arguments.date)){
                offset    = dateDiff("m",now(), arguments.date);
                interval = offset == 1 ? "month":"months";
                result    = "#offset# #interval# left";
            }else if (dateDiff("yyyy", now(), arguments.date)){
                offset    = dateDiff("yyyy", now(), arguments.date);
                interval = offset == 1 ? "year":"years";
                result    = "#offset# #interval# left";
            }
            else{
                // otherwise display date
                result    = formattedDate;
            }       
        }
        else {
            if (dateDiff("s", arguments.date, now()) < 60){
                // less than 1 minute show interval in seconds
                offset    = dateDiff("s", arguments.date, now());
                interval= offset == 1 ? "second":"seconds";
                result    = "#offset# #interval# ago";

            }else if (dateDiff("n", arguments.date, now()) < 60){
                // less than 1 hour show interval in minutes
                offset    = dateDiff("n", arguments.date, now());
                interval= offset == 1 ? "minute":"minutes";
                result    = "#offset# #interval# ago";

            }else if (dateDiff("h", arguments.date, now()) < 24){
                // less than 24 hours display interval in hours
                offset    = dateDiff("h", arguments.date, now());
                interval = offset == 1 ? "hour":"hours";
                result    = "#offset# #interval# ago";

            }else if (dateDiff("d", arguments.date, now()) < 2){
                // less than 2 days display yesterday
                result    = "yesterday";
            }else if (dateDiff("d", arguments.date, now()) < 7){
                // less than 7 days display day
                result    = dayOfWeekAsString( dayOfWeek( arguments.date ));
            }else if (dateDiff("w", arguments.date, now())){
                offset    = dateDiff("w", arguments.date, now());
                interval = offset == 1 ? "week":"weeks";
                result    = "#offset# #interval# ago";
            }else if (dateDiff("m", arguments.date, now())){
                offset    = dateDiff("m", arguments.date, now());
                interval = offset == 1 ? "month":"months";
                result    = "#offset# #interval# ago";
            }else if (dateDiff("yyyy", arguments.date, now())){
                offset    = dateDiff("yyyy", arguments.date, now());
                interval = offset == 1 ? "year":"years";
                result    = "#offset# #interval# ago";
            }
            else{
                // otherwise display date
                result    = formattedDate;
            }
         }
        interval = "<abbr title='" & formattedDate & "'>" & result & "</abbr>";
    }
    return interval;
}

结果是&#34;离开1周&#34;或者&#34; 2周前&#34;。但是,而不是&#34; 2周&#34;,它应该显示我&#34; 1周4天&#34;或者剩下的任何天数,除非它确切地&#34; 2周&#34;。

0 个答案:

没有答案