473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to parse a string to datetime without a time

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
14 21150
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

"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
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
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
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

"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
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

"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
50
by: Doug | last post by:
I can't imagine this is that hard but I'm sure having a struggle finding it. How do I convert a value like "111403" do a DateTime variable in DotNet (i.e. 11/14/2003)?
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
1
by: nb | last post by:
Hi I am using Ole Db to get data from MS Access into my C# windows application. In the database file, there is a column with datatype 'Datetime' and format 'Short Time'. However, in my C#...
0
by: nishi.hirve | last post by:
Hello, I am writing one simple C# .NET application in which I have to use the values retrived from the database. But when I try to retrive value of attribute of type Time without time zone it...
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
7
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
3
by: Peter Duniho | last post by:
I'm sure there's a good explanation for this, but I can't figure it out. I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance, in which I'd replaced the...
2
by: hsachdevah | last post by:
Hi, I developed an application for my study project to do some mathematical calculations. In this application, I am reading from a text file which contains some numbers. Now the problem is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.