Convert Date to Datetime field.

Posted by infant programmer on Stack Overflow See other posts from Stack Overflow or by infant programmer
Published on 2010-04-02T06:10:00Z Indexed on 2010/04/02 6:13 UTC
Read the original article Hit count: 311

Filed under:

The argument my C# function is getting is a string which is merely a Date or a DateTime.

I am suppose to convert this String to DateTime, to carry on furthure calculation.

Now I need to test whether the incoming data is a Date, if it is date(examle:"12/31/2009"), then I need to add "00:00:00" (24 hours format) to it, so that it will become "12/31/2009 00:00:00".

If string manipulation is one possible way, I want to confirm whether there is some other way where we can automate the testing and conversion within DateTime.TryParseExact() method.

This is my sample C# code :
(which is now only able to convert string of format "MM/dd/yyyy HH:mm:ss" to DateTime.)

     private static string[] formats = new string[]
     {
        "MM/dd/yyyy HH:mm:ss"     
     };


     public string date_conv(string date_str)
      {
            DateTime date_value;

            DateTime.TryParseExact(date_str, formats, new global::System.Globalization.CultureInfo("en-US"), global::System.Globalization.DateTimeStyles.None, out date_value);
         /*Some useful instruction to use  date_value*/
return(date_value.ToString("MM/dd/yyyy HH:mm:ss"));
      }

© Stack Overflow or respective owner

Related posts about c#