How to parse a string to datetime without a time

  • Thread starter Thread starter Arjen
  • Start date Start date
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!
 
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);
 
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
 
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);
 
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
 
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.
 
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
 
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
 
Back
Top