publicclass ConvertDateTime
{
///<summary>
///将yyyyMMddHHmmss格式的日期字符串,转为yyyy-MM-dd HH:mm:ss格式的串,如果不能转则返回空串
///</summary>
///<param name="inDateTime">待转换的字符串</param>
///<returns>转换后的字符串</returns>
publicstaticString FormatDataTime(String inDateTime)
{
DateTime outDateTime;
boolflag = DateTime.TryParseExact(inDateTime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None,out outDateTime);
if(flag)
{
return outDateTime.ToString("yyyy-MM-ddHH:mm:ss");
}
else
{
return"";
}
}
///<summary>
///将yyyy-MM-dd HH:mm:ss格式的日期字符串,转为yyyyMMddHHmmss格式的串,如果不能转则返回空串
///</summary>
///<param name="inDateTime">待转换的字符串</param>
///<returns>转换后的字符串</returns>
publicstaticString FormatDataTimeString(String inDateTime)
{
DateTime outDateTime;
boolflag = DateTime.TryParseExact(inDateTime, "yyyy-MM-dd HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None,out outDateTime);
if(flag)
{
return outDateTime.ToString("yyyyMMddHHmmss");
}
else
{
return"";
}
}
///<summary>
/// 将yyyyMMddHHmmss格式的日期字符串,转为日期,如果不能转换返回当前日期
///</summary>
///<param name="inDateTime">待转换的字符串</param>
///<returns>日期</returns>
publicstaticDateTime StringToDateTime(String inDateTime)
{
DateTime outDateTime;
boolflag = DateTime.TryParseExact(inDateTime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None,out outDateTime);
if(flag)
{
return outDateTime;
}
else
{
return DateTime.Now;
}
}
///<summary>
///将yyyy-MM-dd HH:mm:ss格式的日期字符串,转为日期,如果不能转换返回当前日期
///</summary>
///<param name="inDateTime"></param>
///<returns></returns>
publicstaticDateTime ToDateTime(StringinDateTime)
{
DateTime outDateTime;
boolflag = DateTime.TryParseExact(inDateTime, "yyyy-MM-dd HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None,out outDateTime);
if(flag)
{
return outDateTime;
}
else
{
return DateTime.Now;
}
}
}