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!

Getting Date, time and day from string 1

Status
Not open for further replies.

AgentM

MIS
Jun 6, 2001
387
0
0
US
I want to get the year, date and time from a string in the format "2006-11-12 13:00:00.0000". This string comes from a SQL server table. How do I do this?

I refered to thread 732-1210422 which talks about using the SQLparameter class? Are there any examples of this?

Thank you.
 
I may be wrong but I think you could use the string.format functionality to accomplish this. However, if you can't find something to use that way, you could split the string by "-" and use the individual tokens as your value:

ie:
Code:
string sqlServerDateString = "2006-11-12 13:00:00.0000";
string[] splitDateString = sqlServerDateString.Split('-');
string month, year, day, time;
if (splitDateString.Length == 3)
{
   year=splitDateString[0];
   month=splitDateString[1];
   day=splitDateString[2].SubString(0, splitDateString[2].IndexOf(" "));
   time=splitDateString[2].SubString(splitDateString[2].IndexOf(" "));
}
else
{
   //Error
}

Of course there are most likely better ways to do this, but here is an option, if you need one.

Good luck,
-Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
string d = "2006-11-12 13:00:00.0000";

DateTime dt = DateTime.Parse(d);

Might just do the trick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top