fork download
  1. using System;
  2. using System.Globalization;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. CultureInfo provider = CultureInfo.InvariantCulture;
  9.  
  10. // Defining Format and Testing it via "DateTime.ToString(format)"
  11. string format = "MM/dd/yyyy HH:mm:ss tt";
  12. string dtNow = DateTime.Now.ToString (format, provider);
  13. Console.WriteLine (dtNow);
  14.  
  15. // Trying to Parse DateTime on the same Format defined Above
  16. DateTime time;
  17. if (DateTime.TryParseExact (dtNow, format, provider, DateTimeStyles.None, out time))
  18. {
  19. // If TryParseExact Worked
  20. Console.WriteLine ("Result: " + time.ToString ());
  21. }
  22. else
  23. {
  24. // If TryParseExact Failed
  25. Console.WriteLine ("Failed to Parse Date");
  26. }
  27. }
  28. }
Success #stdin #stdout 0.06s 24400KB
stdin
Standard input is empty
stdout
05/05/2015 06:46:23 AM
Result: 5/5/2015 6:46:23 AM