String to DateTime

Water

Newcomer
Joined
Nov 30, 2003
Messages
4
Location
Herceg Novi, Serbia
Hello!

I looked in MSDN help but I couldn't understand how to convert String to DateTime. To be specific i have string "2003:10:31 22:34:52" and I need to convert it to DateTime object. How to do this? Please help.

Best regards,
Water
 
If the date parts were seperated by a character other than a : you could use DateTime.Parse

Visual Basic:
Dim d As DateTime
d = DateTime.Parse("2003/10/31 22:34:52")
 
Yes, but in my case date is not separated by "/". This is not problem, I will write some function to convert, but I ask for later purpose. Is it possible to say Format = "yyyy/dd/mm hh:ss" and to pass this to convert function?
 
Visual Basic:
Dim dateTime As DateTime
Dim dateTimeFormat() As String = {"yyyy/dd/MM hh:mm:ss", "yyyy/dd/MM h:m:s"}
Dim dateTimeString As String = "2004/05/01 5:55:00"
dateTime = dateTime.ParseExact(dateTimeString, dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None)
MessageBox.Show(dateTime.ToString("F"), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
 
Back
Top