You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5中en-AU区域性下DateTimeFormat.FirstDayOfWeek返回错误值的问题及解决方法

Great question! Let's break down why this discrepancy happens and how to fix it in .NET 5.

Why the Difference?

The root cause lies in how globalization data is handled between .NET Framework and .NET 5+:

  • .NET Framework (like 4.7.2) relies on Windows NLS (National Language Support) for regional data, which correctly sets Monday as the first day of the week for the en-AU culture.
  • .NET 5+ uses ICU (International Components for Unicode) by default as its cross-platform globalization engine. Early versions of ICU had a mismatch in the FirstDayOfWeek value for en-AU, incorrectly returning Sunday instead of Monday.

How to Get the Correct Value in .NET 5

Here are three reliable solutions depending on your use case:

1. Switch Back to Windows NLS (Windows-only Apps)

If your application only runs on Windows, you can revert to using the same regional data as .NET Framework. Add this configuration to your .csproj file:

<PropertyGroup>
  <UseSystemResourceKeys>true</UseSystemResourceKeys>
</PropertyGroup>

Alternatively, set the environment variable DOTNET_SYSTEM_GLOBALIZATION_USENLS=true when launching your app. This will make .NET 5 use Windows' NLS data, so FirstDayOfWeek will return Monday for en-AU.

2. Manually Set the First Day of the Week

For cross-platform compatibility, explicitly set the correct FirstDayOfWeek for the en-AU culture:

var auCulture = new CultureInfo("en-AU");
auCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;

// Use this culture instance for date formatting operations
var firstDay = auCulture.DateTimeFormat.FirstDayOfWeek; // Returns Monday

If you want this to apply globally across your app, set it as the default thread culture:

var auCulture = new CultureInfo("en-AU");
auCulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;

CultureInfo.DefaultThreadCurrentCulture = auCulture;
CultureInfo.DefaultThreadCurrentUICulture = auCulture;

3. Upgrade to a Newer .NET Version

Microsoft fixed this ICU data mismatch in later .NET releases (starting with .NET 6). Upgrading to .NET 6 or a newer LTS version will automatically resolve the issue, and you'll get the correct FirstDayOfWeek value for en-AU without any code changes.

内容的提问来源于stack exchange,提问作者Luca Bellavia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 12:37:41