views:

28

answers:

4

I'm using vb.net 2005.

How do convert this date / time 21/08/2008 00:21:00 to a DateTime object ?

+4  A: 

You can use a custom date time format string in conjuction with DateTime.ParseExact or DateTime.TryParseExact.

Dim dateTime as DateTime = _
     DateTime.ParseExact("21/08/2008 00:21:00", "dd/MM/yyyy HH:mm:ss", _ 
     CultureInfo.InvariantCulture)
Oded
@Oded `var` in VB - I think not! ;-)
PhilPursglove
@PhilPursglove - quite right. Anwser updated...
Oded
A: 
Dim d as DateTime = DateTime.Parse("21/08/2008 00:21:00")
Console.WriteLine(d)

produces this:

21/08/2008 12:21:00 a.m.
Preet Sangha
Which may fail if the culture of the computer running this is `fr-FR`, for example.
Oded
true. but there is no mention of culture requirements in the question.
Preet Sangha
A: 

If you want to be sure to parse that format correctly, you should use DateTime.ParseExact with a custom format string matching your pattern (e.g. dd\/MM\/yyyy HH:mm:ss).

Matteo Italia
A: 

I'm using vb.net not c#, my solutions a bit old, but it works

    Dim d1 As Date
    d1 = CDate("21/08/2008 00:21:00")
    Console.WriteLine(d1)
Jules