C# 如何在 yyyymmddhhmmss 中获取日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16080395/
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
How to get DateTime in yyyymmddhhmmss
提问by CSharped
I have written the below query to create an xml from entity, I need to get date time in .NET in yyyymmddhhmmssformat for the field SLOTTINGTIME, so I have thought of writing a new method to get date time in desired format.
我已经编写了下面的查询来从实体创建一个 xml,我需要在 .NET 中yyyymmddhhmmss以字段格式获取日期时间SLOTTINGTIME,所以我想编写一种新方法来以所需格式获取日期时间。
var slottingmessagexml = new XDocument(new XElement("Message",
new XAttribute("ID","SLT"),
new XElement("Record",
new XAttribute("STORENO",slottingmessage.StoreID),
new XAttribute("PICKLOCATION",slottingmessage.PickLocation),
new XAttribute("TPNB",slottingmessage.ProductID),
new XAttribute("SLOTTINGTIME",GetDateTimeInNewFormat(slottingmessage.SlottingDateTime)),
new XAttribute("SLOTTTINGACTION",slottingmessage.SlottingAction))
)
);
采纳答案by Habib
You can use
您可以使用
string strDate = DateTime.Now.ToString("yyyyMMddhhmmss");
If 24hr format is required that use uppercase HHinplace of hhin the format string.
如果需要 24 小时格式HH,请hh在格式字符串中使用大写字母代替。
Remember the first MMshould be in upper caseas lower case mmis for minutes where as uppercase is for Month.
记得第一次MM应该是大写为小写字母mm是几分钟路程为大写是一个月。
For your particular case instead of writing a new method you can do:
对于您的特定情况,您可以执行以下操作,而不是编写新方法:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddhhmmss")),
One more thing to add: The output will contain Hour in 12 hours format because of the lower case hhpart in the string. Not really sure if you need that because without AM/PM this can't indicate the accurate time. For that purpose use HHfor hours which will display hours in 24 hour format. So your code could be:
要添加的另一件事:由于hh字符串中的小写部分,输出将包含 12 小时格式的 Hour 。不确定您是否需要它,因为没有 AM/PM 这不能指示准确的时间。为此,请使用HH小时,它将以 24 小时格式显示小时。所以你的代码可能是:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddHHmmss")),
//^^ for 24 hours format
回答by Ant P
How about this?
这个怎么样?
public string GetDateTimeInNewFormat(DateTime d)
{
return d.ToString("yyyyMMddhhmmss");
}
Or, for 24h format:
或者,对于 24 小时格式:
public string GetDateTimeInNewFormat(DateTime d)
{
return d.ToString("yyyyMMddHHmmss");
}
回答by musefan
Although your question does not explicitly say what format you are looking for, I think that based on your example format of yyyymmddhhmmsswe can assume that you want [years][months][days][hours][minutes][seconds].
虽然您的问题没有明确说明您正在寻找什么格式,但我认为根据您的示例格式,yyyymmddhhmmss我们可以假设您想要[years][months][days][hours][minutes][seconds].
Based on that, we can break each part down as follows:
在此基础上,我们可以将每个部分分解如下:
Years: If you want the full year then your yyyyis correct and will prduce 2013for example. A common (although not encouraged) alternative could be yy(e.g. 13)
年:如果您想要全年,那么您yyyy是正确的,2013例如会产生。一个常见的(虽然不鼓励)替代方案可能是yy(例如13)
Months: Currently your attempt of mmdoes not return months. It will produce the minutes. You likely want MM(e.g. 04). Alternative include MMM(e.g. APR) and MMMM(e.g. April)
月数:目前您的尝试mm不会返回月数。它将产生分钟。您可能想要MM(例如04)。替代包括MMM(例如APR)和MMMM(例如April)
Days: Again you have this correct already. ddwill produce 18for example.
Days: 同样,你已经正确了。dd将产生18例如。
Hours: Your attempt of hhwill produce a 12-hour time format. If this is what you are after then fine. But given that you have not attempted to include an AM/PM designator (with can be done with ttby the way) then I would recommend you opt for the 24-hour format which is HH(upper-case)
小时:您的尝试hh将产生 12 小时的时间格式。如果这就是你所追求的,那很好。但是考虑到您还没有尝试包含 AM/PM 指示符(tt顺便可以使用),那么我建议您选择 24 小时格式,即HH(大写)
Minutes: You are correct with your minutes, mmwill produce 52for example.
分钟:您的分钟是正确的,例如mm将产生52。
Seconds: Again, ssis correct and will produce 33for example.
秒:同样,ss是正确的,将产生33例如。
Now we can string them all together and produce the following format, which includes the 24-hour time format which I would recommend. This can then be passed to the DateTimeobjects ToString()function like so:
现在我们可以将它们串在一起并生成以下格式,其中包括我推荐的 24 小时时间格式。然后可以将其传递给DateTime对象ToString()函数,如下所示:
var stringDateTime = slottingmessage.SlottingDateTime.ToString("yyyyMMddHHmmss");
If you wanted to maintain your approach of the GetDateTimeInNewFormatmethod, then you can implement it like so:
如果你想保持你的方法的GetDateTimeInNewFormat方法,那么你可以像这样实现它:
public string GetDateTimeInNewFormat(DateTime dt)
{
return dt.ToString("yyyyMMddHHmmss");
}
This function would then be called the same way you already have in your example code:
然后将按照您在示例代码中已有的方式调用此函数:
GetDateTimeInNewFormat(slottingmessage.SlottingDateTime)
You can read more information about the various date/time formatting options herewhere there are plenty of examples
您可以在此处阅读有关各种日期/时间格式选项的更多信息,其中有大量示例

