string into DateTime?

Y

Yin99

I have the string "8/2/2007 12:00:00 AM"

I'd like to get this in DateTime variable so I can manipulate (e.g.
use TimeSpan to find difference). However, when I look at the
DateTime constructors, everything seems to be int, long, etc.

So my question is, what would be the easiest way to get this into
DateTime? Seems like a long way would be to split at the "/", parse
out, use int.Parse, etc (brute force). But I was wondering if there
was a quick and cleaver way to do this in minimal lines of code?

Thanks- Yin

background info: i'm using DateTimePicker control and reading the
value which returns a string in format above. I have two
DateTimePickers and need to know the number of months between the
value in the first DateTimePicker and the second DateTimePicker.
 
R

RB

Yin99 said:
I have the string "8/2/2007 12:00:00 AM"

I'd like to get this in DateTime variable so I can manipulate (e.g.
use TimeSpan to find difference). However, when I look at the
DateTime constructors, everything seems to be int, long, etc.

So my question is, what would be the easiest way to get this into
DateTime? Seems like a long way would be to split at the "/", parse
out, use int.Parse, etc (brute force). But I was wondering if there
was a quick and cleaver way to do this in minimal lines of code?

Thanks- Yin

background info: i'm using DateTimePicker control and reading the
value which returns a string in format above. I have two
DateTimePickers and need to know the number of months between the
value in the first DateTimePicker and the second DateTimePicker.

I think the following code will work:

Dim d As DateTime
d = Date.Parse("8/2/2007 12:00:00 AM")

Cheers,

RB.
 
P

Petar Atanasov

Yin99 said:
I have the string "8/2/2007 12:00:00 AM"

I'd like to get this in DateTime variable so I can manipulate (e.g.
use TimeSpan to find difference). However, when I look at the
DateTime constructors, everything seems to be int, long, etc.

So my question is, what would be the easiest way to get this into
DateTime? Seems like a long way would be to split at the "/", parse
out, use int.Parse, etc (brute force). But I was wondering if there
was a quick and cleaver way to do this in minimal lines of code?

Thanks- Yin

background info: i'm using DateTimePicker control and reading the
value which returns a string in format above. I have two
DateTimePickers and need to know the number of months between the
value in the first DateTimePicker and the second DateTimePicker.

Try out DateTime.ParseExact(...)
http://msdn2.microsoft.com/en-us/library/w2sa9yss.aspx

HTH,
Petar Atanasov
http://a-wake.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top