【问题标题】:Comparing DOB to date today比较今天的 DOB
【发布时间】:2016-08-23 17:02:35
【问题描述】:

我正在尝试检查年龄,以便如果客户未满 23 岁,我可以显示注释,知道如何执行此操作吗?我正在使用代码优先实体框架。

【问题讨论】:

    标签: asp.net-mvc entity-framework ef-code-first


    【解决方案1】:

    非常直接:

    var under23 = DateTime.Today.AddYears(-23);
    var clients = db.Clients.Where(m => m.ClientDOB > under23);
    

    基本上,您只是从当前日期减去 23 年,然后查询 DOB 大于该日期的客户,即他们的出生日期大于 23 年前,使他们小于 23 岁。

    【讨论】:

      【解决方案2】:

      这是你可以使用的逻辑

          DateTime date=DateTime.Now;
          var expectedMinDate=date.AddYears(-23);
          DateTime clientBirthDate=Convert.ToDateTime("23/08/1993");
          if(clientBirthDate>expectedMinDate)
          {
          Console.Write("Smaller than 23");
          }
          else
          {
              Console.Write("Greater than or equal to 23");
          }
      

      【讨论】:

        【解决方案3】:
            DateTime birthday = new DateTime(1995, 7, 30);
            var age = GetAge(birthday);
        
            if (age < 23)
            {
                 // do something
            }
        
            public int GetAge(DateTime dob)
            {
                var today = DateTime.Today;
                var age = today.Year - dob.Year;
                if (dob > today.AddYears(-age))
                    age--;
        
                return age;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-29
          • 1970-01-01
          • 2011-01-08
          • 2015-07-24
          • 2020-01-04
          相关资源
          最近更新 更多