UWP 更改 DateTimeFormatInfo.DateSeparator 等

UWP change DateTimeFormatInfo.DateSeparator etc

在 "normal" .NET 中,您可以更改 DateTimeFormatInfo.DateSeparator,但此 属性 在 UWP 变体中不再是 public(并且 DateTimeFormatInfo 已密封) .其他一些属性也无法访问。

如何按照我想要的方式创建格式设置的 DateTimeFormatInfo?我的目标是拥有一个 "what-our-project-considers-invariant" CultureInfo,其中“-”作为 DateSeparator,所有日期的格式为 "yyyy'-'MM'-'dd",而不是 "invariant" 以月份开头的日期格式。

有什么想法吗?

谢谢!

如您所见,DateTimeFormatInfo.DateSeparator Property is not available in UWP apps. In UWP apps, for formatting a date or time, we usually use DateTimeFormatter class。有了这个class,我们可以根据用户的时间和语言设置.

格式化日期或时间

如果你想使用格式 "yyyy-MM-dd",你可以像这样设置格式化程序:

var datefmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("{year.full}-{month.integer(2)}-{day.integer(2)}");

var dateString = datefmt.Format(DateTime.Now);

您也可以使用 Standard Date and Time Format Strings,例如:

var dateString = DateTime.Now.ToString("yyyy-MM-dd");

有关详细信息,请参阅 GitHub 上的 Format dates and times and Date and time formatting sample