How to parse a string to datetime without a time

A

Arjen

Hi,

I have a string with this value 07/11/2008. As you see no time included.

How can I parse this string to a datetime variable? I tried to do this but I
get the exception that the format is incorrect.

Any ideas?

I tried both convert... and datetime.par...

Thanks again!
 
A

Arjen

Peter Duniho said:
Did you try the ParseExact() method? I would expect that to work fine.

I tried this but it did not work.
DateTime.ParseExact(Request.Form["date"], @"MM\/dd\/YYYY", null);
 
A

Arne Vajhøj

Arjen said:
Peter Duniho said:
Did you try the ParseExact() method? I would expect that to work fine.

I tried this but it did not work.
DateTime.ParseExact(Request.Form["date"], @"MM\/dd\/YYYY", null);

Try with yyyy instead of YYYY.

Arne
 
A

Arjen

Peter Duniho said:
I tried this but it did not work.
DateTime.ParseExact(Request.Form["date"], @"MM\/dd\/YYYY", null);

No, of course that wouldn't. :) First, you are trying to escape
characters in a string that you've declared as an unescaped literal.
Second, you need lower-case y's, not upper-case.

Try:

DateTime.ParseExact(Request.Form["date"], "MM/dd/yyyy", null);

Note that since a forward slash is not a special string formatting
character, you don't need escaping _or_ the use of the @ symbol for the
string.

Pete


Hmm... maybe I'm blind... I still get the error message. :(

string data = "07/17/2008";

DateTime date = DateTime.ParseExact(data, "MM/dd/yyyy", null);

Response.Write(date);
 
A

Arne Vajhøj

Arjen said:
Peter Duniho said:
I tried this but it did not work.
DateTime.ParseExact(Request.Form["date"], @"MM\/dd\/YYYY", null);

No, of course that wouldn't. :) First, you are trying to escape
characters in a string that you've declared as an unescaped literal.
Second, you need lower-case y's, not upper-case.

Try:

DateTime.ParseExact(Request.Form["date"], "MM/dd/yyyy", null);

Note that since a forward slash is not a special string formatting
character, you don't need escaping _or_ the use of the @ symbol for
the string.

Hmm... maybe I'm blind... I still get the error message. :(

string data = "07/17/2008";

DateTime date = DateTime.ParseExact(data, "MM/dd/yyyy", null);

Response.Write(date);

Keep the escaping.

/ in a DateTime format string means current date separator and
may not be '/' - with my settings it is '-'.

Arne
 
Q

qglyirnyfgfo

Sorry for the stupid question but….

I have never seen this date time formatting character. I searched the
date time formatting strings and was not able to find anything
regarding “\”.

I am kind of lost here, what exactly does having the “\”character
does?

Thanks.
 
A

Arne Vajhøj

Sorry for the stupid question but….

I have never seen this date time formatting character. I searched the
date time formatting strings and was not able to find anything
regarding “\”.

I am kind of lost here, what exactly does having the “\”character
does?

We were discussing forward slash not back slash.

But both has a special meaning.

And both are listed at:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Arne
 
C

Cor Ligthert[MVP]

Peter,

(on English
systems it likely would be, but for other cultures, it could be something
else, like a hyphen or a period, for example).
You probably mean (on US systems.................?

AFAIK is by instance the UK confirm the most used European behaviour in this
(this excluding countries where the ISO 8601 in Europe is used like by
instance Sweden and Russia)

While the rest of the English speaking world including Canada uses the UK
system.

Although we are in Europe not very consistent in this, sometimes we use a
slash, a hyphen, a dot or just a space.

Cor
 

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

Similar Threads

Parse DateTime and Boolean 6
Convert this string to a datetime 1
Time conversion 2
Parsing DateTime 10
DataTime - TryParseExact 3
String to DateTime 1
SqlDateTime and DateTime 2
XMLElement and Type Parse. 1

Top