显示日期,例如:“1年,10个月”

时间:2014-03-08 12:13:20

标签: javascript jquery angularjs

我正在使用Angularjs,想知道如何以这种“格式”显示日期

成员:1 year, 10 months

是否有Javascript/Jquery/Angularjs内置函数正在执行此操作,或者我应该自行计算。

谢谢

2 个答案:

答案 0 :(得分:1)

您可以创建自己的过滤器,将.fromNow()包裹在moment.js

filter('timeago', function() {
    return function(datetime) {
        return moment(datetime).fromNow();
    };
})

然后你可以像任何其他角度过滤器一样使用它;

{{ "2014-01-01T12:00:00" | timeago }}

Demo

答案 1 :(得分:0)

由于我没有找到我想要的东西,所以我自己做了。

所以这个函数返回两个日期之间的时间。

Date.prototype.from = function(pastDate, presentDate){

    var presentDate = (typeof presentDate === "undefined") ? this : presentDate;

    //the string to return.
    var dateString = "";

    var minute = 60 * 1000,
        hour = minute * 60,
        day = hour * 24,
        month = day * 30,
        year = month * 12,
        ms = Math.abs(presentDate - pastDate);

    var years = parseInt(ms / year, 10);
    ms -= years * year;

    var months = parseInt(ms / month, 10);
    ms -= months * month;

    var days = parseInt(ms / day, 10);
    ms -= days * day;

    var hours = parseInt(ms / hour, 10);
    ms -= hours * hour;

    var minutes = parseInt(ms / minute, 10);

     if(months <= 0 && days <= 0 && hours <= 0 && minutes <= 0){
        dateString = "1 minute";
        return dateString;
     }

     if(years > 0){

        if(years == 1){
            dateString += years + " year";
        }else{
            dateString += years + " years";
        }

        if(months > 0){

            if(months == 1){
                dateString += ", " + months + " month";
            }else{
                dateString += ", " + months + " months";
            }

        }
     //year = 0
     }else{
         if(months > 0){

            if(months == 1){
                dateString += months + " month";
            }else{
                dateString += months + " months";
            }

            if(days > 0){

                if(days == 1){
                    dateString  += ", " + days + " day";
                }else{
                    dateString  += ", " + days + " days";
                }
            }

         //month = 0
         }else{
            if(days > 0){

                if(days == 1){
                    dateString += days + " day";
                }else{
                    dateString += days + " days";
                }

                if(hours > 0){

                    if(hours == 1){
                        dateString += ", " + hours + " hour";
                    }else{
                        dateString += ", " + hours + " hours";
                    }

                }
            //days = 0
            }else{
                if(hours > 0){

                    if(hours == 1){
                        dateString += hours + " hour";
                    }else{
                        dateString += hours + " hours";
                    }
                    if(minutes > 0){

                        if(minutes == 1){
                            dateString += minutes + " minute";
                        }else{
                            dateString +=", " + minutes + " minutes";
                        }
                    }
                //hours = 0
                }else{
                    if(minutes > 0){

                        if(minutes == 1){
                            dateString += minutes + " minute";
                        }else{
                            dateString += minutes + " minutes";
                        }
                    }
                }
            }
         }
     }

    return dateString
}

<强> USAGE

// now
var now = new Date();

var d1 = new Date("October 13, 1995 11:13:00")
var d2 = new Date("March 13, 2005 11:13:00")

// from now
alert( now.from( d1 ) )

// time elapsed between two dates 
alert( now.from( d1, d2 ) )

FIDDLE