該方法可以驗證帶格式的的日期

 該方法可以驗證帶格式的的日期

格式在:string[] date = strIn.Split(new char[]{'格式'},格式可以指定,比如‘-’,則要驗證的日期必須符合2006-10-10這種格式。

        //驗證 年-月-日 
        public static bool IsValidDate(string strIn) 
        

            
bool result = false;
            
if(strIn.Length <= 10 && strIn.Length >=8)
            
{
                
string[] date = strIn.Split(new char[]{'-'});
                
if(date.Length == 3)
                
{
                    
try
                    
{
                        
int year = Convert.ToUInt16(date[0],10);
                        
int month = Convert.ToInt16(date[1],10);
                        
int day = Convert.ToInt16(date[2],10);
                        result 
= CheckIsValidDate(day,month,year);
                    }

                    
catch
                    
{
                        result 
= false;
                    }

                }

            }

            
return result;
        }
  
    private static bool CheckIsValidDate(int day, int month, int year)
        
{
            
if(month > 12 || month < 1)
                
return false;
            
if(day > 31 || day < 1)
                
return false;
            
if(day > DateTime.DaysInMonth(year, month))
                
return false;
            
return true;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章