Chris Chris - 26 days ago 13
C# Question

Converting time to Military

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();

Answer Source

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?