views:

195

answers:

3

Maybe I'm seeing things..

In an attempt to turn a date with a format of "mm/dd/yyyy hh:mm:ss PM" into military time, the following replacement of a row value does not seem to take. Even though I am sure I have done this before (with column values other than dates). Is there some reason that row["adate"] would not accept a value assigned to it in this case?

DateTime oos = DateTime.Parse(row["adate"].ToString());

row["adate"] =  oos.Month.ToString() 
              + "/" 
              + oos.Day.ToString() 
              + "/" 
              + oos.Year.ToString() 
              + " " 
              + oos.Hour.ToString() 
              + ":" 
              + oos.Minute.ToString();
+3  A: 

Try

row["adate"].Text = oos.ToString("MM/dd/YYYY HH:mm");
Theresa
+4  A: 

Beside first answer check this:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Andrey
Yeah, and all the people posting their MM/dd/yyyy solutions should go their too, and have a look on http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#dateSeparator and the differences between for example MM and M
prostynick
+1  A: 

Instead of formatting the string manually, you should use:

oos.ToString("M/d/yyyy HH:mm");

Also, what do you mean by "would not accept a value"? Are you getting an exception? If so, what is the error message?

Lucas
Thanks! DateTime has a ToString that takes a format, excellent.
Chris