Advertisement
Guest User

ParseDateTime()

a guest
Aug 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. private DateTime ParseDateTime()
  2.         {
  3.             try
  4.             {
  5.                 if (m_Value is DateTime)
  6.                     return (DateTime)m_Value;
  7.                 else if (m_Value is TimeSpan)
  8.                 {
  9.                     TimeSpan ts = (TimeSpan)m_Value;
  10.                     return new DateTime(ts.Ticks);
  11.                 }
  12.                 return Convert.ToDateTime(m_Value);
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 // this is the format exception - we will test
  17.                 // one of most common date representations - [YY]YYMMDD
  18.                 string strText = m_Value.ToString();
  19.  
  20.                 try
  21.                 {
  22.                     return DateTime.ParseExact(
  23.                         strText,
  24.                         new string[] {
  25.                             "MM/dd/yy",
  26.                             "MM/dd/yyyy",
  27.                             "yyyyMMdd",
  28.                             "yyMMdd",
  29.                             "yyyy-MM-dd",
  30.                             "yyyy-MM-ddTHH:mm:ss",
  31.                             "HHmm",
  32.                             "HHmmss",
  33.                             "HHmmssfff",
  34.                             "HH:mm",
  35.                             "HH:mm:ss",
  36.                             "HH:mm:ss:fff",
  37.                             "HH:mm:ss.fff",
  38.                             "MMM dd HH:mm",
  39.                             "MMM dd yyyy"
  40.                             },
  41.                         CultureInfo.InvariantCulture,
  42.                         DateTimeStyles.None
  43.                         );
  44.                 }
  45.                 catch
  46.                 {
  47.                     throw ex;   // the format was not confirmed - rethrow original exception
  48.                 }
  49.             }
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement