Reputation: 503
I try to display a DateTime
with the "general date short time" format.
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
.
It seems to duplicate the time and I don't know why.
Anyone?
Edit 1 Here is the code I use:
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":
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!
Upvotes: 4
Views: 1203
Reputation: 3542
The "d" format specifier applies the short date pattern and "g" is a concatenation of the short date and short time patterns. 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? If yes, maybe there's a mistake in that code somewhere.
Upvotes: 2
Reputation: 1286
Try this
startDate = DateTime.Now.ToString(System.Globalization.CultureInfo.
CurrentCulture.DateTimeFormat);
Upvotes: 0
Reputation: 145
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
Upvotes: 1