使用过滤器以自定义格式格式化日期

时间:2014-03-14 15:35:28

标签: javascript angularjs mongodb date

我试图在我的角度应用上显示一些自定义格式的日期:我想看“1小时前”,“2天前”......而不是实际时间所以我在考虑使用过滤器来做到这一点。

我的日期与MongoDb一起存储,所以我收到它们为ISODate:

ISODate("2014-03-13T10:48:02.991Z")

基本上我想将此功能应用到我的日期:

function timeSince(ts){
    now = new Date();
    ts = new Date(ts*1000);
    var delta = now.getTime() - ts.toString().getTime();

    delta = delta/1000; //us to s

    var ps, pm, ph, pd, min, hou, sec, days;

    if(delta<=59){
        ps = (delta>1) ? "s": "";
        return delta+" second"+ps
    }

    if(delta>=60 && delta<=3599){
        min = Math.floor(delta/60);
        sec = delta-(min*60);
        pm = (min>1) ? "s": "";
        ps = (sec>1) ? "s": "";
        return min+" minute"+pm+" "+sec+" second"+ps;
    }

    if(delta>=3600 && delta<=86399){
        hou = Math.floor(delta/3600);
        min = Math.floor((delta-(hou*3600))/60);
        ph = (hou>1) ? "s": "";
        pm = (min>1) ? "s": "";
        return hou+" hour"+ph+" "+min+" minute"+pm;
    } 

    if(delta>=86400){
        days = Math.floor(delta/86400);
        hou =  Math.floor((delta-(days*86400))/60/60);
        pd = (days>1) ? "s": "";
        ph = (hou>1) ? "s": "";
        return days+" day"+pd+" "+hou+" hour"+ph;
    }

}

如何使用过滤器完成此操作?

由于

3 个答案:

答案 0 :(得分:0)

你正在寻找一个基本的角度滤镜模板吗? 这就是你应该用角度过滤器包装代码的方法。

angular.module('MyModule', []).
  filter('timeSince', function() {
    return function(input) {

    var now = new Date();
    var ts = new Date(input);
    var delta = now.getTime() - ts.toString().getTime();

    delta = delta/1000; //us to s

    var ps, pm, ph, pd, min, hou, sec, days;

    if(delta<=59){
        ps = (delta>1) ? "s": "";
        return delta+" second"+ps
    }

    if(delta>=60 && delta<=3599){
        min = Math.floor(delta/60);
        sec = delta-(min*60);
        pm = (min>1) ? "s": "";
        ps = (sec>1) ? "s": "";
        return min+" minute"+pm+" "+sec+" second"+ps;
    }

    if(delta>=3600 && delta<=86399){
        hou = Math.floor(delta/3600);
        min = Math.floor((delta-(hou*3600))/60);
        ph = (hou>1) ? "s": "";
        pm = (min>1) ? "s": "";
        return hou+" hour"+ph+" "+min+" minute"+pm;
    } 

    if(delta>=86400){
        days = Math.floor(delta/86400);
        hou =  Math.floor((delta-(days*86400))/60/60);
        pd = (days>1) ? "s": "";
        ph = (hou>1) ? "s": "";
        return days+" day"+pd+" "+hou+" hour"+ph;
    }

    return "-";
    };
  });

在标记中使用:时间:{{this_should_be_the_ISODate_from_model | timeSince}}

答案 1 :(得分:0)

您只需要创建一个返回该功能的filter

myModule.filter('prettyTime', function(){

   return function(ts){
      //Your code here
   };

});

假设你$scope某个地方有一些Date对象:

<span>{{time | prettyTime}}</span>

这是一个有效的演示:http://jsfiddle.net/jwcarroll/ARc65/

答案 2 :(得分:0)

使用Moment.js,这很简单:

App.filter('ago', function(){
  return function(epoch){
    var diff = Date.now() - parseInt(epoch,10);
    return moment.duration(diff).humanize() + ' ago';
  };
});

Moment.js on CDNJS

编辑:Moment还将解析各种ISO类型,请参阅docs

用法是@Josh提到的:

<span>{{ time | ago }}</span>