[英]DateTime.ToString issue
I try to display a DateTime
with the "general date short time" format. 我尝试使用“常规日期短时间”格式显示
DateTime
。 When I use the g
specifier, it gives me something like 01-08-13 10:12:00 10:12
instead of 01-05-13 10:12
. 当我使用
g
说明符时,它给我一些像01-08-13 10:12:00 10:12
而不是01-05-13 10:12
。
It seems to duplicate the time and I don't know why. 它似乎复制了时间,我不知道为什么。
Anyone? 任何人?
Edit 1 Here is the code I use: 编辑1这是我使用的代码:
var startDate = DateTime.MinValue.ToString("g");
if (Airspace.StartDate != null)
startDate = ((DateTime)Airspace.StartDate).ToString("g"); //01-08-13 00:00:00 00:00
Edit 2 The same issue occurs when I use "short date pattern": 编辑2当我使用“短日期模式”时,会出现同样的问题:
var startDate = DateTime.MinValue.ToString("d");
if (Airspace.StartDate != null)
startDate = ((DateTime)Airspace.StartDate).ToString("d"); //01-08-13 00:00:00
It doesn't make sense! 这没有意义!
The "d" format specifier applies the short date pattern and "g" is a concatenation of the short date and short time patterns. “d”格式说明符应用短日期模式,“g”是短日期和短时间模式的串联。 So based on your results, you somehow have a short date pattern with time components in it.
因此,根据您的结果,您会以某种方式使用包含时间组件的短日期模式。 I can reproduce your results by setting such a short date pattern explicitly, like so:
我可以通过明确设置这样一个短日期模式来重现您的结果,如下所示:
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd HH:mm:ss";
Console.WriteLine(DateTime.Today.ToString("g")); // 2008-05-11 00:00:00 00:00
Console.WriteLine(DateTime.Today.ToString("d")); // 2008-05-11 00:00:00
I think the real question is how you ended up with what looks like some very strange culture settings! 我认为真正的问题是你如何看待一些非常奇怪的文化背景! I tried enumerating the set of supported cultures and looking for one whose short date format included time specifiers, like so:
我尝试枚举一组受支持的文化,并寻找一个短日期格式包含时间说明符的文化,如下所示:
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)
.OrderByDescending(c => c.DateTimeFormat.ShortDatePattern.Length))
{
Console.WriteLine($"{culture.Name} {culture.DateTimeFormat.ShortDatePattern}");
}
But in several hundred cultures I came up with nothing. 但在几百种文化中,我什么也没想到。 So, can you find anything anywhere in your code that's building up a
CultureInfo
object and assigning it as the current culture? 那么,你能在代码中找到构建
CultureInfo
对象并将其指定为当前文化的任何地方吗? If yes, maybe there's a mistake in that code somewhere. 如果是的话,也许在那个代码中有一个错误。
Hope this can help you: 希望这可以帮到你:
DateTime today = DateTime.Now;
Console.WriteLine(today.ToString("dd-MM-yy H:mm"));
//Result: 01-08-13 04:33
Console.ReadLine();
Other format: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm 其他格式: http : //www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
Try this 试试这个
startDate = DateTime.Now.ToString(System.Globalization.CultureInfo.
CurrentCulture.DateTimeFormat);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.