473,471 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question of formatting a string to DateTime

I am being passed a string value in the form of MMddyyyy that needs to be
converted to DateTime. Do I need to first convert the string to MM/dd/yyyy
before calling the DateTime.Parse(string) method?
Nov 16 '05 #1
6 4705
Nope, just use DateTime.Parse(). It should parse it without problems.

HTH,

Kyril

"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be
converted to DateTime. Do I need to first convert the string to MM/dd/yyyy
before calling the DateTime.Parse(string) method?

Nov 16 '05 #2
You can have space characters though to parse using
DateTimeStyles.AllowWhiteSpaces.

In your case, you may have to insert "/". If month and day are switched, you
have to specify FormatProvider in the argument.

IFormatProvider culture = new CultureInfo("en-US", true);

Try this

string myDate= "02 22 2004";
IFormatProvider culture = new System.Globalization.CultureInfo("en-US",
true);
DateTime a =
DateTime.Parse(myDate,culture,System.Globalization .DateTimeStyles.AllowWhite
Spaces);

--
Shak
(Houston)
"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be
converted to DateTime. Do I need to first convert the string to MM/dd/yyyy
before calling the DateTime.Parse(string) method?

Nov 16 '05 #3
Hi Bill,
The best way of do it is using DateTime.ParseExact() this method let you
especify the format of the string being converted, in your case you can try

DateTime.ParseExact( stringToConvert,
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be
converted to DateTime. Do I need to first convert the string to MM/dd/yyyy
before calling the DateTime.Parse(string) method?

Nov 16 '05 #4
Sounds like I will have to insert / or space. For example, if I'm passed
12102001 I will need to convert it to either 12/10/2001 or 12 10 2001.

Not difficult, but I was thinking there might be something more natural,
such as a format string.

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
You can have space characters though to parse using
DateTimeStyles.AllowWhiteSpaces.

In your case, you may have to insert "/". If month and day are switched, you have to specify FormatProvider in the argument.

IFormatProvider culture = new CultureInfo("en-US", true);

Try this

string myDate= "02 22 2004";
IFormatProvider culture = new System.Globalization.CultureInfo("en-US",
true);
DateTime a =
DateTime.Parse(myDate,culture,System.Globalization .DateTimeStyles.AllowWhite Spaces);

--
Shak
(Houston)
"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be converted to DateTime. Do I need to first convert the string to MM/dd/yyyy before calling the DateTime.Parse(string) method?


Nov 16 '05 #5
Hi

Sorry but I sent it without the code

The code looks like:
DateTime.ParseExact( stringToConvert, , "MMddyyyy", null );

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be
converted to DateTime. Do I need to first convert the string to MM/dd/yyyy
before calling the DateTime.Parse(string) method?

Nov 16 '05 #6
Ah, perfect. That's exactly what I was hoping for... although a little
surprised they didn't overload this option for DateTime.Parse().

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:es****************@TK2MSFTNGP09.phx.gbl...
Hi

Sorry but I sent it without the code

The code looks like:
DateTime.ParseExact( stringToConvert, , "MMddyyyy", null );

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Bill" <nf*@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
I am being passed a string value in the form of MMddyyyy that needs to be converted to DateTime. Do I need to first convert the string to MM/dd/yyyy before calling the DateTime.Parse(string) method?


Nov 16 '05 #7

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

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
4
by: Andrew Wilhite | last post by:
Hello, I am just learning C# and I have ran into a problem that I cannot seem to resolve. I need to capture today's date, subtract a day, and then put it into the MM-DD-YYYY format. I know...
2
by: Mark | last post by:
I am running into a weird problem. I am using the following code: String.Format("{0:0}:{1:00}:{2:00}", Hours.ToString, Minutes.ToString, Seconds.ToString) Just say seconds is set to 1, it...
6
by: SQL Server | last post by:
I've been working this for a while. Kind of new to SQL Server functions and not seeing what I am doing wrong. I have this function CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS...
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
4
by: Joseph | last post by:
Hi all- I am a former VB6 programmer and new at C# and I have a question dealing with converting some code from VB6 to C#. The code is below and essentially, what it does is gets data from a SQL...
16
by: Bruce W. Darby | last post by:
I've almost completed my little application for work. This weekend I've been working on Streams so I can write a logfile showing the work that was accomplished. Wanting to make each logfile...
2
by: csharpula csharp | last post by:
Hello, I am trying to add Date and Current time to a name of a log file. I tried doing this: string logDateExtention = DateTime.Now.ToShortDateString().ToString() +...
3
by: Tim | last post by:
Folks, I'm trying to format a print string so that it reports progress whilst processing a looping structure with a date time stamp appended to the end of the string. This started out life as a...
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...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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 project—planning, coding, testing,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.