Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. DateTime expectedDate;
  2. if (!DateTime.TryParse("07/27/2012", out expectedDate))
  3. {
  4. Console.Write("Luke I am not your datetime.... NOOO!!!!!!!!!!!!!!");
  5. }
  6.  
  7. string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy" };
  8. DateTime expectedDate;
  9. if (!DateTime.TryParseExact("07/27/2012", formats, new CultureInfo("en-US"),
  10. DateTimeStyles.None, out expectedDate))
  11. {
  12. Console.Write("Thank you Mario, but the DateTime is in another format.");
  13. }
  14.  
  15. private bool IsValidDateFormat(string dateFormat)
  16. {
  17. try
  18. {
  19. String dts=DateTime.Now.ToString(dateFormat);
  20. DateTime.ParseExact(dts, dateFormat, CultureInfo.InvariantCulture);
  21. return true;
  22. }
  23. catch (Exception)
  24. {
  25. return false;
  26. }
  27. }
  28.  
  29. DateTime.ParseExact("qq", "qq", null) == DateTime.Today
  30. DateTime.ParseExact("myy", "501", null) == "05/01/2001"
  31.  
  32. d,D,f,F,g,G,m,M,o,O,r,R,s,T,u,U,y,Y
  33.  
  34. try
  35. {
  36. String formattedDate = DateTime.Now.ToString(dateFormat);
  37. DateTime.Parse(formattedDate);
  38. return true;
  39. }
  40. catch (Exception)
  41. {
  42. return false;
  43. }
Add Comment
Please, Sign In to add comment