【问题标题】:Convert birthday to age in angularjs在angularjs中将生日转换为年龄
【发布时间】:2014-09-13 00:47:59
【问题描述】:

我想在网格中显示我所有用户的年龄。我正在从 facebook 读取数据。我没有将其存储在任何地方。

我正在显示日期:

{{ friend.birthday }}

我怎样才能显示年龄而不是显示生日。

如果可以创建过滤器而不是如何创建过滤器以及如何应用它。

【问题讨论】:

    标签: angularjs angularjs-filter


    【解决方案1】:

    你可以实现一个函数:

    控制器:

    $scope.calculateAge = function calculateAge(birthday) { // birthday is a date
        var ageDifMs = Date.now() - birthday.getTime();
        var ageDate = new Date(ageDifMs); // miliseconds from epoch
        return Math.abs(ageDate.getUTCFullYear() - 1970);
    }
    

    HTML

    {{ calculateAge(friend.birthday) }}
    

    或过滤器:

    app.filter('ageFilter', function() {
         function calculateAge(birthday) { // birthday is a date
             var ageDifMs = Date.now() - birthday.getTime();
             var ageDate = new Date(ageDifMs); // miliseconds from epoch
             return Math.abs(ageDate.getUTCFullYear() - 1970);
         }
    
         return function(birthdate) { 
               return calculateAge(birthdate);
         }; 
    });
    

    HTML

    {{ friend.birthday | ageFilter }}
    

    年龄算法取自SO answer

    [EDIT] 如果年龄小于1岁,又想显示月份,可以修改ageFilter计算月差:

    app.filter('ageFilter', function() {
         function calculateAge(birthday) { // birthday is a date
             var ageDifMs = Date.now() - birthday.getTime();
             var ageDate = new Date(ageDifMs); // miliseconds from epoch
             return Math.abs(ageDate.getUTCFullYear() - 1970);
         }
         function monthDiff(d1, d2) {
           if (d1 < d2){
            var months = d2.getMonth() - d1.getMonth();
            return months <= 0 ? 0 : months;
           }
           return 0;
         }       
         return function(birthdate) { 
               var age = calculateAge(birthdate);
               if (age == 0)
                 return monthDiff(birthdate, new Date()) + ' months';
               return age;
         }; 
    });
    

    Demo Plunker - Age Function
    Demo Plunker - Age Filter
    Demo Plunker - Age Filter with Months < 1 year

    【讨论】:

    • 效果很好。感谢您提供此解决方案。但是,如果婴儿的年龄小于 1 岁,而不是将年龄显示为“0”,我该如何修改它以使其以月份为单位显示,例如 6 months 的年龄?
    • 更新的答案包括婴儿月份
    • 此解决方案在某些情况下不起作用。它在 2016 年 1 月 26 日(日.月.年)为 1.1.2015 生日输出 0 年。
    • 它确实有效...月份从索引 0 开始。所以,第 0 个月是 1 月,第 1 个月是 2 月。您是否误以为 1 是 1 月?
    • 为什么是geDate.getUTCFullYear() - 1970
    【解决方案2】:

    如果您使用的是 momentjs。然后你可以简单地使用这个 sn-p 来创建过滤器

    var now  = "04/09/2013 15:00:00";
    var then = "04/09/2013 14:20:30";
    
    moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")
    

    【讨论】:

      【解决方案3】:

      如果您的价值只是例如“05/01/2016”。这将是一个将日期转换为生日的有用代码。

      AngularJS

      app.filter('ageFilter', function(){
          return function(birthday){
              var birthday = new Date(birthday);
              var today = new Date();
              var age = ((today - birthday) / (31557600000));
              var age = Math.floor( age );
              return age;
          }
      });
      

      HTML

      {{ relationTypePreDefined.birthdate | ageFilter }}
      

      顺便说一句,我使用此解决方案将来自 jquery datepicker 输入的日期转换为年龄。

      【讨论】:

        【解决方案4】:

        我知道为什么我永远不能回复别人,说我需要更多的代表,但代表我需要发表评论.. 随便。

        作为对@Dean Christian Armada 的回应,我不断收到有关过滤器的错误消息。但更改为以下似乎工作正常,所以我很感激!

        $scope.getAge = function(birthday){
            var birthday = new Date(birthday);
            var today = new Date();
            var age = ((today - birthday) / (31557600000));
            var age = Math.floor( age );
            return age;
          }
        

        对于 HMTL

        {{ getAge(birthdate) }}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-17
          • 2020-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多