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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to date time conversion

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
0
0
US
Hello,
I am trying to convert a string to datetime using the following code -

foreach (DataRow dRow in ds.Tables["task"].Rows)
{
DateTime runTime = Convert.ToDateTime(dRow["time"].ToString());

Console.WriteLine(runTime);

}

the value that is returned is in the format "MM/dd/yyyy HH:mm".
I am getting the error "String was not recognized as a valid DateTime"

Anyone have any idea what could be happening?
 
I think you may need to use dRow["item"].Value.ToString(), but I'm not sure on that. I think dRow["item"].ToString might return the type name.

If this is stored as a DateTime in your database though (and it should be!) I would think that you can put dRow["item"].Value directly into a DateTime object.

If it is in fact a string in your database, you won't always have a valid date time, so you might have a look at the Parse or TryParse methods in the DateTime class.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
I am not extracting it from a database. I am parsing an xml file and loading it into a dataset.
I do not have the option to choose .value. If I add the line Console.WriteLine(dRow["time"].ToString());, it does display the correct value from the xml file.

I tried using the Parse and ParseExact, but am still getting the same error.
 
Looks like you will need to do some string manipulation to get it into an acceptable format. I've never needed to go from date to string, so I can't really help you there. But keep in mind your locality settings, they play a big role in what's acceptable for date conversion.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Have you looked at the XmlConvert class?

Also, XML should be using the ISO-8601 format that is defined by the W3C (but few people know about it, it seems).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I have already fed the xml into a dataset, so I am not actually working with xml data.
 
I think you need to validate the data returned from XML file before parsing it.
You can validate the dRow["time"].ToString() by using DateTime regex as per your requirement. Then pass that validated string to convert it to DateTime.
You can have Regex pattern for this from regexlib.com

Sharing the best from my side...

--Prashant--
 
you may also want to try directly casting the object to a datetime
Code:
foreach (DataRow dRow in ds.Tables["task"].Rows)
{
   DateTime runTime = (DateTime)dRow["time"];
   Console.WriteLine(runTime);
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top