I would like to output a DateTime as a string in the format specified in the operating system preferences. I don't want to provide an explicit format pattern, but instead want to use whatever the user has defined in the Region panel.
For example on my Windows 10 system, formats are defined under Control Panel > Region > Formats as follows:
- Short date: "yyyy-MM-dd"
- Short time: "HH:mm"
When files and folders are displayed in Windows Explorer, they appear in the format: "yyyy-MM-dd HH:mm".
I should point out that I'm using Unity 2019.4.4. -- As far as I know, it's not doing anything strange by overriding the CurrentCulture or anything like that. I've tagged this question with the "unity3d" tag, but it may not be specifically related to Unity, unless Unity is doing strange things with the culture.
I've tried the following .NET Fiddle, and saw the same results as in Unity:
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
long dateTimeTicks = 637314588165245627;
DateTime dateTime = new DateTime(dateTimeTicks);
// I thought the following methods might work, since the Region panel allow configuring the
// short and long formats for date and time.
Console.WriteLine(dateTime.ToShortDateString()); // 7/27/2020
Console.WriteLine(dateTime.ToLongDateString()); // Monday, July 27, 2020
Console.WriteLine(dateTime.ToShortTimeString()); // 3:00 PM
Console.WriteLine(dateTime.ToLongTimeString()); // 3:00:16 PM
// None of the standard date and time format strings work. The "o", "s", and "u" are close to
// what I'm looking for, but I don't want to force this format, I want to use whatever the
// user has specified in the Region preferences.
Console.WriteLine(dateTime.ToString()); // 7/27/2020 3:00:16 PM
Console.WriteLine(dateTime.ToString(CultureInfo.CurrentCulture)); // 7/27/2020 3:00:16 PM
Console.WriteLine(dateTime.ToString(CultureInfo.InvariantCulture)); // 07/27/2020 15:00:16
Console.WriteLine(dateTime.ToString(CultureInfo.InstalledUICulture)); // 7/27/2020 3:00:16 PM
Console.WriteLine(dateTime.ToString("d")); // 7/27/2020
Console.WriteLine(dateTime.ToString("D")); // Monday, July 27, 2020
Console.WriteLine(dateTime.ToString("f")); // Monday, July 27, 2020 3:00 PM
Console.WriteLine(dateTime.ToString("F")); // Monday, July 27, 2020 3:00:16 PM
Console.WriteLine(dateTime.ToString("g")); // 7/27/2020 3:00 PM
Console.WriteLine(dateTime.ToString("G")); // 7/27/2020 3:00:16 PM
Console.WriteLine(dateTime.ToString("m")); // July 27
Console.WriteLine(dateTime.ToString("o")); // 2020-07-27T15:00:16.5245627
Console.WriteLine(dateTime.ToString("r")); // Mon, 27 Jul 2020 15:00:16 GMT
Console.WriteLine(dateTime.ToString("s")); // 2020-07-27T15:00:16
Console.WriteLine(dateTime.ToString("t")); // 3:00 PM
Console.WriteLine(dateTime.ToString("T")); // 3:00:16 PM
Console.WriteLine(dateTime.ToString("u")); // 2020-07-27 15:00:16Z
Console.WriteLine(dateTime.ToString("U")); // Monday, July 27, 2020 3:00:16 PM
Console.WriteLine(dateTime.ToString("y")); // July 2020
// I considered using CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern and
// CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern together, but they also
// don't return what is specified in Region preferences.
// 7/27/2020
Console.WriteLine(dateTime.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern));
}
}
I'm assuming I need to somehow access the Culture information that's defined in the OS Regions panel and use that to format the string, but that's where I'm lost.
The CurrentCulture seems to be using a ShortDate of "M/d/yyyy" and a ShortTime of "h:mm tt", which is not what I have set in my Region panel.
How can I output the DateTime as a string in the format specified by the user and stored in the Region panel on Windows without knowing what that pattern is ahead of time?
Ultimately, I would like the string to be in the same format used by Windows Explorer, which would be something like "ShortDate ShortTime".
DateTime.Now.ToString(CultureInfo.CurrentCulture)
returns "Tue 28-Jul-20 7:13:20 AM". CallingCultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
andCultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern
also returns their correct values. – AlaCultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
ignoring your custom format set on Region panel, fill a bug – Ala