Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  4. Dim str = "1/4/19" ' test data '
  5.  
  6. If cbDateFormat.SelectedIndex = -1 Then
  7. MsgBox("Please select the date format.")
  8. Exit Sub
  9. End If
  10.  
  11. Dim dateFormat = ""
  12.  
  13. Select Case cbDateFormat.Text
  14. Case "DD/MM/YYYY"
  15. dateFormat = "d/M/yyyy"
  16. Case "MM/DD/YYYY"
  17. dateFormat = "M/d/yyyy"
  18. Case "DD/MM/YY"
  19. dateFormat = "d/M/yy"
  20. Case "MM/DD/YY"
  21. dateFormat = "M/d/yy"
  22. Case Else
  23. Throw New Exception("Date format not found.")
  24. End Select
  25.  
  26. Dim d As DateTime
  27. If DateTime.TryParseExact(str, dateFormat, Globalization.CultureInfo.InvariantCulture, Nothing, d) Then
  28. ' d now holds the datetime '
  29. MsgBox(d.ToString("dd-MMM-yyyy"))
  30. End If
  31.  
  32. End Sub
  33.  
  34. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  35. cbDateFormat.Items.AddRange({"DD/MM/YYYY", "MM/DD/YYYY", "DD/MM/YY", "MM/DD/YY"})
  36.  
  37. End Sub
  38.  
  39. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement