日期时间格式,如 HH:mm 24 小时,不含 AM/PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/360982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime Format like HH:mm 24 Hours without AM/PM
提问by Jonathan Escobedo
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.
我在这里搜索有关将“16:20”之类的字符串转换为 DateTime 类型而不丢失格式的信息,我说我不想添加 dd/MM/yyy 或秒或 AM/PM,因为 db 只接受这种格式。
I tried with Cultures yet
我尝试过文化
Thanks in Advance
提前致谢
采纳答案by John Sheehan
All DateTime objects must have a date and a time.
所有 DateTime 对象都必须具有日期和时间。
If you want just the time, use TimeSpan:
如果您只想要时间,请使用 TimeSpan:
TimeSpan span = TimeSpan.Parse("16:20");
If you want a DateTime, add that time to the min value:
如果您需要 DateTime,请将该时间添加到最小值:
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
回答by MartinHN
DateTime.Now.ToString("hh:mm") - If it's C#.
DateTime.Now.ToString("hh:mm") - 如果是 C#。
Oh. Only read the header.
哦。只读取标题。
DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
回答by Joel Coehoorn
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.
但是,当您检索该值时,您会将其转换回您想要的任何格式。如果你愿意,HH:mm
你可以得到它。
回答by Victor
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");
如果将其转换为 DateTime 类型,则 DateTime 对象将具有 dd/mm/yy 和其他属性。根据您计划如何使用该对象,您可以“恢复”您的原始设置,方法是格式化字符串输出,如下所示: DT.ToString("HH:mm");
回答by Jonathan Leffler
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.
由于您没有规定您使用的是哪个 DBMS,因此很难知道哪个答案对您有帮助。如果您使用 IBM Informix Dynamic Server,您只需使用数据类型“DATETIME HOUR TO MINUTE”,它将以 24 小时制记录值。
回答by Jimmy
DateTime.Parse("16:20")
回答by Arno
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 Hwill give you the 24 hours time format
and lowerCase "h" will give you the 12 hours time
format...
string DateFormat = "yyyy MM d "
这会给你年月日。继续后;
string DateFormat = "yyyy MM d HH:mm:ss "
在这里,大写 H将为您提供24 hours time format
小写"h" will give you the 12 hours time
格式...
when you give the Dateformat as a string you can do whatever you want with date and time.
当您将 Dateformat 作为字符串提供时,您可以对日期和时间执行任何操作。
string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);
OR
或者
Console.writeline(DateTime.Now.ToStrign(DateFormat));
OUTPUT:
输出:
20120823132544