判断字符串HHmmss是否是有效的字符串时间

2015-12-08 09:44:06  访问(1745) 赞(0) 踩(0)


        /// <summary>
        /// 判断字符串HHmmss是否是有效的字符串时间
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool TimeIsValidStr(string str)
        {
            if (str == null || str.Length == 0)
                return false;

            str = str.Trim();

            if(str.Length ==0)
                return false;

            int iLen = str.Length;

            if (iLen != 6)
                return false;

            string strH = str.Substring(0, 2);
            string strM = str.Substring(2, 2);
            string strS = str.Substring(4, 2);

            int theValue = -1;

            if (!int.TryParse(strH, out theValue))
                return false;

            if (theValue < 0 || theValue > 23)
                return false;

            if (!int.TryParse(strM, out theValue))
                return false;

            if (theValue < 0 || theValue > 59)
                return false;

            if (!int.TryParse(strS, out theValue))
                return false;

            if (theValue < 0 || theValue > 59)
                return false;

            return true;
        }


标签:DateTime的操作    C#    校验    时间判断    TimeIsValidStr    HHmmss 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)