473,549 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21178
On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmai l.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*********@nn owslpianmk.coms chreef in bericht
news:op******** *******@petes-computer.local. ..
On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmai l.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.ParseE xact(Request.Fo rm["date"], @"MM\/dd\/YYYY", null);

Jul 3 '08 #3
Arjen wrote:
"Peter Duniho" <Np*********@nn owslpianmk.coms chreef in bericht
news:op******** *******@petes-computer.local. ..
>On Thu, 03 Jul 2008 12:14:52 -0700, Arjen <bo*****@hotmai l.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.ParseE xact(Request.Fo rm["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*****@hotmai l.comwrote:
I tried this but it did not work.
DateTime.ParseE xact(Request.Fo rm["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.ParseE xact(Request.Fo rm["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*********@nn owslpianmk.coms chreef in bericht
news:op******** *******@petes-computer.local. ..
On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <bo*****@hotmai l.comwrote:
>I tried this but it did not work.
DateTime.Parse Exact(Request.F orm["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.ParseE xact(Request.Fo rm["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.ParseE xact(data, "MM/dd/yyyy", null);

Response.Write( date);

Jul 3 '08 #7
Arjen wrote:
"Peter Duniho" <Np*********@nn owslpianmk.coms chreef in bericht
news:op******** *******@petes-computer.local. ..
>On Thu, 03 Jul 2008 12:48:05 -0700, Arjen <bo*****@hotmai l.comwrote:
>>I tried this but it did not work.
DateTime.Pars eExact(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.ParseE xact(Request.Fo rm["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.ParseE xact(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.d kschreef in bericht
news:48******** *************** @news.sunsite.d k...
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.d kwrote:
> Hmm... maybe I'm blind... I still get the error message. :(
string data = "07/17/2008";
DateTime date = DateTime.ParseE xact(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
3138
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 their types. I would like to somehow store a reference to parsing operations in this array (such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc),...
50
3954
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
14076
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). so i wonder is there any faster way to parse string(char array) into float number.
1
4703
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# application, it is displayed as a date. I tried changing oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("DepartureTime",
0
1114
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 generates following exception {"Index was outside the bounds of the array."} System.Exception {System.IndexOutOfRangeException}
3
5190
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> nothing special as you can see. there is a part of my code that converts <somefloatvalue to float. for that i use float.Parse(string)... and i end up...
6
8545
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 circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is...
7
3054
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
21237
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 DateTimeFormatInfo.FullDateTimePattern property with my custom format string: DateTimeFormatInfo dtfi = ...
2
2825
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 that it works correctly on my system but not on some other systems. When I debugged the application on the system where it was not working I found that...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.