Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using Convert.ToDateTime function

Status
Not open for further replies.

Sielah

Programmer
Oct 9, 2007
50
CA
I have a date format conversion problem that I just can't find a solution for.

Here is the code snippet I'm using :

CultureInfo culture = new CultureInfo("en-us");
DateTime rate_dt = Convert.ToDateTime(txtStartDate.Text.ToString(),culture);

When I run the program, I enter "19-May-2008" in txtStartDate.

Unfortunately, the conversion always produces a rate_dt value of '19-05-2008 00:00:00'.

This format causes massive headaches for us. What I need is for rate_dt to contain either '05-19-2008 00:00:00' or '2008-05-19 00:00:00'.

I have tried running the code under every available Date format in my Regional Settings section, but nothing seems to work.

Anybody have any advice?
 
a date is an object, formatting shouldn't matter except for display(form, webpage, print).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
What I need is for rate_dt to contain either '05-19-2008 00:00:00' or '2008-05-19 00:00:00'.

It actually contains both.
:)

The deal is that a DateTime struct is just a container for a date, and doesn't have any rules about how it is to be displayed. That's the job of an IFormatProvider (of which CultureInfo is one). When viewing a DateTime in the debugger, Visual Studio is the one providing the formatter.

So rest assured, the date was read correctly. You just need to tell it how to display it when shown to the user.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top