Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. string str = "1st February 2013";
  2. DateTime dtObject;
  3. string replacedStr = str.Substring(0,4)
  4. .Replace("nd","")
  5. .Replace("th","")
  6. .Replace("rd","")
  7. .Replace("st","")
  8. + str.Substring(4);
  9.  
  10.  
  11. if (DateTime.TryParseExact(replacedStr,
  12. "dd MMMMM yyyy",
  13. CultureInfo.InstalledUICulture,
  14. DateTimeStyles.None,
  15. out dtObject))
  16. {
  17. //valid date
  18. }
  19.  
  20. string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
  21. "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
  22. "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
  23. "M/d/yyyy h:mm", "M/d/yyyy h:mm",
  24. "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
  25. string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
  26. "5/1/2009 6:32:00", "05/01/2009 06:32",
  27. "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"};
  28. DateTime dateValue;
  29.  
  30. foreach (string dateString in dateStrings)
  31. {
  32. if (DateTime.TryParseExact(dateString, formats,
  33. new CultureInfo("en-US"),
  34. DateTimeStyles.None,
  35. out dateValue))
  36. Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
  37. else
  38. Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement