Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Format like HH:mm 24 Hours without AM/PM

I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.

I tried with Cultures yet

Thanks in Advance

like image 641
Jonathan Escobedo Avatar asked Dec 11 '08 21:12

Jonathan Escobedo


4 Answers

Just give a date format to your dateTime.

string DateFormat = "yyyy MM d " this willl give you the year month and day. after continuing; string DateFormat = "yyyy MM d HH:mm:ss " in here the Capital H will give you the 24 hours time format and lowerCase "h" will give you the 12 hours time format...

when you give the Dateformat as a string you can do whatever you want with date and time.

string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);

OR

Console.writeline(DateTime.Now.ToStrign(DateFormat));

OUTPUT:

20120823132544
like image 174
Arno Avatar answered Oct 14 '22 15:10

Arno


All DateTime objects must have a date and a time.

If you want just the time, use TimeSpan:

TimeSpan span = TimeSpan.Parse("16:20");

If you want a DateTime, add that time to the min value:

TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
like image 34
John Sheehan Avatar answered Oct 14 '22 14:10

John Sheehan


DateTime.Now.ToString("hh:mm") - If it's C#.

Oh. Only read the header.

DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
like image 29
MartinHN Avatar answered Oct 14 '22 15:10

MartinHN


what do you mean by "losing the format".

if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");

like image 30
Victor Avatar answered Oct 14 '22 14:10

Victor