.日期和字符串直接的转换方法

/// <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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值