/// <summary>
/// 将日期转换成“XXXX年XX月XX日”字符串
/// </summary>
/// <param name="dt">日期对象</param>
/// <Author;>tx:2011-10-12</Author>
private string DateTimeToYMD(DateTime dt)
{
try
{
string strDT = string.Empty ;
if (dt.Year.ToString() == null)
{
return null;
}
strDT = dt.Year.ToString();
strDT += "年";
if (dt.Month.ToString() == null)
{
return strDT;
}
strDT += dt.Month.ToString();
strDT += "月";
if (dt.Day.ToString() == null)
{
return strDT;
}
strDT += dt.Day.ToString();
strDT += "日";
return strDT;
}
catch(Exception ex)
{
return string.Empty;
throw ex;
}
}
/// <summary>
/// 将日期转换成“XXXX年XX月XX日 XX时XX分”字符串
/// </summary>
/// <param name="dt">日期对象</param>
/// <Author;>tx:2011-10-12</Author>
private string DateTimeToYMDHM(DateTime dt)
{
try
{
string strDT = string.Empty;
if (dt.Year.ToString() == null)
{
return null;
}
strDT = dt.Year.ToString();
strDT += "年";
if (dt.Month.ToString() == null)
{
return strDT;
}
strDT += dt.Month.ToString();
strDT += "月";
if (dt.Day.ToString() == null)
{
return strDT;
}
strDT += dt.Day.ToString();
strDT += "日";
if(dt.Hour.ToString()==null)
{
return strDT;
}
strDT += " ";
strDT += dt.Hour.ToString();
strDT += "时";
if (dt.Minute.ToString()==null)
{
return strDT;
}
strDT += dt.Minute.ToString();
strDT += "分";
if (dt.Millisecond.ToString() == null)
{
return strDT;
}
strDT += dt.Millisecond.ToString();
strDT += "秒";
return strDT;
}
catch (Exception ex)
{
return string.Empty;
throw ex;
}
}
/// <summary>
/// 日期转化成星期几
/// </summary>
/// <param name="dt">日期对象</param>
/// <Author;>tx:2011-10-12</Author>
private string DateTimeToWeek(DateTime dt)
{
try
{
string strDate = string.Empty;
switch (dt.DayOfWeek.ToString())
{
case "Sunday":
strDate = "星期日";
break;
case "Monday":
strDate = "星期一";
break;
case "Tuesday":
strDate = "星期二";
break;
case "Wednesday":
strDate = "星期三";
break;
case "Thursday":
strDate = "星期四";
break;
case "Friday":
strDate = "星期五";
break;
case "Saturday":
strDate = "星期六";
break;
default:
break;
}
return strDate;
}
catch (Exception ex)
{
return string.Empty;
throw ex;
}
}
/// <summary>
/// 字符串转日期,对于很多种字符串日期类型都能转成DateTime类型
/// </summary>
/// <param name="strDateTime">日期字符串类型</param>
/// <Author;>tx:2011-10-12</Author>
private DateTime StringToDataTime(string strDateTime)
{
string[] strFormat = new string[] { "yyyy/MM/dd", "yyyy-MM-dd", "yyyy年MM月dd日","yyyyMMdd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd H:mm:ss",
"yyyy/MM/dd H:m:ss", "yyyy/MM/dd HH:m:ss", "yyyy/MM/dd HH:mm:s", "yyyy/MM/dd H:m:s", "yyyy/M/dd", "yyyy/M/d", "yyyy-M-dd",
"yyyy-MM-d", "yyyy-M-d", "yyyy年M月dd日", "yyyy年MM月d日", "yyyy年M月d日", "yyyy年M月d日 HH时mm分", "yyyy年M月d日 H时mm分",
"yyyy年M月d日 HH时m分", "yyyy年M月d日 H时m分" , "yyyy-MM-dd H:mm:ss" , "yyyy-MM-dd HH:m:ss" , "yyyy-MM-dd HH:mm",
"yyyy-MM-dd H:mm", "yyyy-MM-dd H:m", "yyyy-MM-dd H:m:s", "yyyy-MM-dd HH:m:s", "yyyy-MM-dd H:m:ss","yyyy/MM","yyyy-MM",
"yyyy年MM月","yyyy/M","yyyy-M","yyyy年M月"};
try
{
DateTime dt;
for (int i = 0; i < strFormat.Length; i++)
{
if (DateTime.TryParseExact(strDateTime, strFormat[i], System.Globalization.CultureInfo.CurrentCulture, DateTimeStyles.None, out dt))
{
return dt;
}
}
}
catch (Exception ex)
{
throw ex;
}
return DateTime.MinValue;
}