object转成DateTime?输出
2014-12-31 15:36:15 访问(1486) 赞(0) 踩(0)
/// <summary>
/// object转成DateTime?输出
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public DateTime? DateTimeNullConvertTo(object obj)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
return null;
string str = obj.ToString();
if (str.Length == 0)
return null;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "obj == " + str + " 不是有效的时间值。"
);
}
/// <summary>
/// object转成DateTime?输出,不符合条件则返回defaultValue
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public DateTime? DateTimeNullConvertTo(object obj, DateTime? defaultValue)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
return null;
string str = obj.ToString();
if (str.Length == 0)
return null;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
标签:
object转成DateTime?输出 


上一条:
下一条:
相关评论
发表评论