Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting time to Military

Tags:

c#

datetime

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();
like image 408
Chris Avatar asked Mar 10 '10 13:03

Chris


1 Answers

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?

like image 67
Lucas Avatar answered Sep 29 '22 18:09

Lucas