By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,279 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,279 IT Pros & Developers. It's quick & easy.

How to parse a string to datetime without a time

P: n/a
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!

Jul 3 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmail.comwrote:
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.
Did you try the ParseExact() method? I would expect that to work fine.
Jul 3 '08 #2

P: n/a

"Peter Duniho" <Np*********@nnowslpianmk.comschreef in bericht
news:op***************@petes-computer.local...
On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmail.comwrote:
>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.

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);

Jul 3 '08 #3

P: n/a
Arjen wrote:
"Peter Duniho" <Np*********@nnowslpianmk.comschreef in bericht
news:op***************@petes-computer.local...
>On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmail.comwrote:
>>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.

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
Jul 3 '08 #4

P: n/a
On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <bo*****@hotmail.comwrote:
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
Jul 3 '08 #5

P: n/a
Peter Duniho wrote:
Note that since a forward slash is not a special string formatting
character,
It is a special DateTime formatting character !

Arne
Jul 3 '08 #6

P: n/a

"Peter Duniho" <Np*********@nnowslpianmk.comschreef in bericht
news:op***************@petes-computer.local...
On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <bo*****@hotmail.comwrote:
>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);

Jul 3 '08 #7

P: n/a
Arjen wrote:
"Peter Duniho" <Np*********@nnowslpianmk.comschreef in bericht
news:op***************@petes-computer.local...
>On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <bo*****@hotmail.comwrote:
>>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
Jul 3 '08 #8

P: n/a

"Arne Vajhøj" <ar**@vajhoej.dkschreef in bericht
news:48***********************@news.sunsite.dk...
Peter Duniho wrote:
>Note that since a forward slash is not a special string formatting
character,

It is a special DateTime formatting character !

Arne
Aha! Thanks... now it works. :)

Jul 3 '08 #9

P: n/a
On Thu, 03 Jul 2008 13:19:38 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
> 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.
And the @ (the string won't compile otherwise). Alternatively, use the
invariant culture instead of "null".

Sorry, I overlooked the issue Arne's talking about, as my own system
settings use / for the date separator.

Pete
Jul 3 '08 #10

P: n/a
On Thu, 03 Jul 2008 13:14:52 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
Peter Duniho wrote:
>Note that since a forward slash is not a special string formatting
character,

It is a special DateTime formatting character !
Ah, right. Good point.
Jul 3 '08 #11

P: n/a
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.


On Jul 3, 3:14*pm, Arne Vajhj <a...@vajhoej.dkwrote:
Peter Duniho wrote:
Note that since a forward slash is not a special string formatting
character,

It is a special DateTime formatting character !

Arne
Jul 3 '08 #12

P: n/a
qg**********@mailinator.com wrote:
On Jul 3, 3:14 pm, Arne Vajhj <a...@vajhoej.dkwrote:
>Peter Duniho wrote:
>>Note that since a forward slash is not a special string formatting
character,
It is a special DateTime formatting character !
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
Jul 3 '08 #13

P: n/a
On Thu, 03 Jul 2008 14:46:39 -0700, <qg**********@mailinator.comwrote:
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?
It escapes the forward slash so that the DateTime parser knows to expect
an exact match in the input string, rather than treating the forward slash
as the special character that it is (as Arne pointed out :) ).

Without the escaping, the parser will treat the forward slash as a
placeholder representing whatever the current culture's date separator
character is, which may or may not actually be a forward slash (on English
systems it likely would be, but for other cultures, it could be something
else, like a hyphen or a period, for example).

Pete
Jul 3 '08 #14

P: n/a
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

Jul 4 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.