有18位或15位

解决方案 »

  1.   

                String str = "256954198806182158";            if (str.Length == 18)
                {
                    str = str.Substring(6,8);
                    DateTime dt;
                    if (DateTime.TryParseExact(str, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out dt))
                    {
                        if (DateTime.Now.Subtract(dt).Days / 365 > 18)
                            Console.WriteLine("18");
                    }
                }
      

  2.   

    下面是我的方法貌似效率不高
    public string GetUserState(string num)
        {//420802198807222351
            string state = "";
            if (num != "")
            {
                string d="";
                TimeSpan ts = new TimeSpan();
                DateTime dt = new DateTime();
                if (num.Length == 18) 
                {
              d=  num.Substring(6,8);
               dt = DateTime.ParseExact(d, "yyyyMMdd",null);
          
                    
                }
                else if(num.Length==15)
                {
                    d = num.Substring(6, 6);
                     dt = DateTime.ParseExact(d, "yyMMdd", null);
                   
                }
                ts = DateTime.Now.Subtract(dt);
                DateTime now = DateTime.Now;
                if ((now.Year - dt.Year) > 18)
                {
                    state = "已成年,未纳入防沉迷系统";
                }
                else
                {
                    if (now.Month >= dt.Month && now.Day >= dt.Day)
                    {
                        state = "已成年,未纳入防沉迷系统";
                    }
                    else
                    {
                        state = "未成年,纳入防沉迷系统";
                    }
                }
                //state = ts.ToString();        }
            return state;    }
      

  3.   

    写个例子吧string str=substring("211320198310312116",7,8);
    string da=DataTime.Now.ToString("yyMMdd");
    int i=Convert.ToInt32(str);
    int j=Convert.ToInt32(da);
    int k=j-i;
    if (k>18)
    messagebox.show("成年");
    else
    messagebox.show("未成年");
      

  4.   

    public string GetUserState(string num)
        {//420802198807222351
            string state = "";
            if (num != "")
            {
                string d="";
                TimeSpan ts = new TimeSpan();
                DateTime dt = new DateTime();
                if (num.Length == 18) 
                {
              d=  num.Substring(6,8);
               dt = DateTime.ParseExact(d, "yyyyMMdd",null);
          
                    
                }
                else if(num.Length==15)
                {
                    d = num.Substring(6, 6);
                     dt = DateTime.ParseExact(d, "yyMMdd", null);
                   
                }
                ts = DateTime.Now.Subtract(dt);
                DateTime now = DateTime.Now;
                if ((now.Year - dt.Year) > 18)
                {
                    state = "已成年,未纳入防沉迷系统";
                }
                else
                {
                    if (now.Month >= dt.Month && now.Day >= dt.Day)
                    {
                        state = "已成年,未纳入防沉迷系统";
                    }
                    else
                    {
                        state = "未成年,纳入防沉迷系统";
                    }
                }
                //state = ts.ToString();        }
            return state;    }
      

  5.   

    总体算法如下:获取生日,
    用生日和当前时间,
    比较看看是否大于18。每一步都不难,分解一下把:
    获取生日就是截取字符串,
    比较就用时间函数,如果不要求严格,可以用年份相减我正在使用《Csdn收音机》第一时间获取最新动态!
      

  6.   

    最终代码,刚才后面忘记了判断 public string GetUserState(string num)
        {//420802198807222351
            string state = "";
            if (num != "")
            {
                string d="";
                TimeSpan ts = new TimeSpan();
                DateTime dt = new DateTime();
                if (num.Length == 18) 
                {
              d=  num.Substring(6,8);
                    
               dt = DateTime.ParseExact(d, "yyyyMMdd",null);
          
                    
                }
                else if(num.Length==15)
                {
                    d = num.Substring(6, 6);
                     dt = DateTime.ParseExact(d, "yyMMdd", null);
                   
                }
                ts = DateTime.Now.Subtract(dt);
                DateTime now = DateTime.Now;
                if ((now.Year - dt.Year) > 18)
                {
                    state = "已成年,未纳入防沉迷系统";
                }
                else
                {
                    if (now.Month > dt.Month )
                    {
                        state = "已成年,未纳入防沉迷系统";
                    }
                    else
                    {
                        if (now.Day >= dt.Day)
                        {
                            state = "已成年,未纳入防沉迷系统";
                        }
                        else 
                        {
                            state = "未成年,纳入防沉迷系统";
                        }
                    }
                }
                //state = ts.ToString();        }
            return state;    }