我正在尝试确定
DataGridView
在哪里获取其默认格式字符串,以将 DateTime
转换为 String
。
如屏幕截图所示,在我的系统上使用的日期格式是 yyyy-MM-dd h:mm tt。不显示秒。然而,查看各种
CultureInfo
对象,没有单一的日期格式匹配。
对于给定的
DateTime
,可以使用以下方式生成匹配的输出字符串:
DateTime d = DateTime.Now;
var ci = CultureInfo.CurrentCulture;
var df = ci.DateTimeFormat;
String s = d.ToString(df.ShortDatePattern) + " " + d.ToString(df.ShortTimePattern);
然而,这是否真的是值得怀疑的。如果使用单一格式,而不是用空格连接两种单独的格式,那就更有意义了。或者这就是正在做的事情?
public static void TestDgv() {
DateTime now = DateTime.Now;
DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, AllowUserToAddRows = false, AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells };
DataTable table = new DataTable();
table.Columns.Add("DateTimeColumn", typeof(DateTime));
table.Rows.Add(new Object[] { now });
dgv.DataSource = table;
Form f = new Form();
f.Controls.Add(dgv); // displays format yyyy-MM-dd h:mm tt
Application.Run(f); // e.g. 2024-10-30 4:29 PM
StringBuilder sb = new StringBuilder();
System.Globalization.CultureInfo c1 = System.Globalization.CultureInfo.CurrentCulture;
System.Globalization.CultureInfo c2 = System.Globalization.CultureInfo.CurrentUICulture;
System.Globalization.CultureInfo c3 = System.Globalization.CultureInfo.InvariantCulture;
foreach (var ci in new [] { c1, c2, c3 }) {
sb.AppendLine();
sb.AppendLine(ci == c1 ? "CurrentCulture" : ci == c2 ? "CurrentUICulture" : "InvariantCulture");
DateTimeFormatInfo d = ci.DateTimeFormat;
String s1 = d.FullDateTimePattern;
String s2 = d.LongDatePattern;
String s3 = d.LongTimePattern;
String s4 = d.MonthDayPattern;
String s5 = d.ShortDatePattern;
String s6 = d.ShortTimePattern;
String s7 = d.SortableDateTimePattern;
String s8 = d.UniversalSortableDateTimePattern;
String s9 = d.YearMonthPattern;
foreach (String ss in new [] { s1, s2, s3, s4, s5, s6, s7, s8, s9 }) {
sb.AppendLine(now.ToString(ss) + "\t" + ss);
}
}
}
/*
CurrentCulture
Wednesday, October 30, 2024 4:29:46 PM dddd, MMMM dd, yyyy h:mm:ss tt
Wednesday, October 30, 2024 dddd, MMMM dd, yyyy
4:29:46 PM h:mm:ss tt
30 October d MMMM
2024-10-30 yyyy-MM-dd
4:29 PM h:mm tt
2024-10-30T16:29:46 yyyy'-'MM'-'dd'T'HH':'mm':'ss
2024-10-30 16:29:46Z yyyy'-'MM'-'dd HH':'mm':'ss'Z'
October, 2024 MMMM, yyyy
CurrentUICulture
Wednesday, October 30, 2024 4:29:46 PM dddd, MMMM d, yyyy h:mm:ss tt
Wednesday, October 30, 2024 dddd, MMMM d, yyyy
4:29:46 PM h:mm:ss tt
October 30 MMMM d
10-30-2024 M/d/yyyy
4:29 PM h:mm tt
2024-10-30T16:29:46 yyyy'-'MM'-'dd'T'HH':'mm':'ss
2024-10-30 16:29:46Z yyyy'-'MM'-'dd HH':'mm':'ss'Z'
October 2024 MMMM yyyy
InvariantCulture
Wednesday, 30 October 2024 16:29:46 dddd, dd MMMM yyyy HH:mm:ss
Wednesday, 30 October 2024 dddd, dd MMMM yyyy
16:29:46 HH:mm:ss
October 30 MMMM dd
10-30-2024 MM/dd/yyyy
16:29 HH:mm
2024-10-30T16:29:46 yyyy'-'MM'-'dd'T'HH':'mm':'ss
2024-10-30 16:29:46Z yyyy'-'MM'-'dd HH':'mm':'ss'Z'
2024 October yyyy MMMM
*/
不确定您是否尝试过,但设置
CultureInfo.DefaultThreadCurrentCulture
和/或 CultureInfo.DefaultThreadCurrentUICulture
具有您可能想要的效果。请参阅下面的代码示例:
[STAThread]
static void Main()
{
var culture = CultureInfo.GetCultureInfo("en-US");
//Culture for any thread
CultureInfo.DefaultThreadCurrentCulture = culture;
//Culture for UI in any thread
CultureInfo.DefaultThreadCurrentUICulture = culture;
DateTime now = DateTime.Now;
DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, AllowUserToAddRows = false, AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells };
DataTable table = new DataTable();
table.Columns.Add("DateTimeColumn", typeof(DateTime));
table.Rows.Add(new Object[] { now });
dgv.DataSource = table;
Form f = new Form1();
f.Controls.Add(dgv);
Application.Run(f);
}
结果:
拥有
pl-PL
文化
拥有
en-US
文化