Datetime Format

  • Thread starter Thread starter Harikumar G
  • Start date Start date
H

Harikumar G

Friends,

My hardware sends me a date format like this

15:10:20.99 UTC Mon Oct 18 2004

I need to process this format and parse to a valid SQL datetime format, how
can I do it more easily? My front-end language is C#

-Hari
 
Hi Har,

If you have a DateTime object, use the SqlDateTime constructor.

If you only have a string, use the SqlDateTime.Parse method.

HTH,
Rakesh Rajan
 
Hari,

You will need to get rid of the "UTC" bit in your input string before this
can be parsed by the SqlDateTime or DateTime classes. See the code below
which shows an example of how to get both, including converting from DateTime
back to the string format used for dates in SQL queries.

// this is the input format:
string inDateStr = "15:10:20.99 UTC Mon Oct 18 2004";

// remove the UTC bit
string parseDateStr = inDateStr.Replace("UTC", "");

// do this to create a SqlDateTime
SqlDateTime sdt = SqlDateTime.Parse(parseDateStr);
string sdtStr = sdt.ToString(); // and check the format

// do the following to create DateTime, and extract a string
// in the SQL datetime string format:
System.DateTime dt = DateTime.Parse(parseDateStr);
string dateStr = dt.ToString("yyyy-MM-dd HH:mm:ss");

HTH,
Chris.
 
Back
Top