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

2020-03-12 07:51发布

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

7条回答
Melony?
2楼-- · 2020-03-12 08:19

I want to address this part of your question:

without losing the format

A database will generally store all datetime values in a standard common format that's not even human readable. If you use a datetime column the original format is destroyed.

However, when you retrieve the value you cast it back to any format you want. If you want HH:mm you can get it.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-03-12 08:27

Since you don't stipulate which DBMS you are using, it is hard to know which answer will help you. If you use IBM Informix Dynamic Server, you would simply use the data type 'DATETIME HOUR TO MINUTE', which will record values in the 24 hour clock.

查看更多
够拽才男人
4楼-- · 2020-03-12 08:31

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);
查看更多
唯我独甜
5楼-- · 2020-03-12 08:37

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
查看更多
混吃等死
6楼-- · 2020-03-12 08:38

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

查看更多
家丑人穷心不美
7楼-- · 2020-03-12 08:39

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
查看更多
登录 后发表回答