スレッド カルチャに少し問題があり、日付を正しく表示することができません。DateTime クラスの ToString() メソッドをオーバーロードしています。
文化が「en-CA」の場合、日付は正しい形式「yyyy/MM/dd」で表示されますが、文化が「fr-CA」の場合、日付は「yyyy-MM-dd」になります。
問題を表示するためにいくつかの単体テストを作成しました。英語のテストは機能しますが、フランス語は常に失敗します。
GetDateInStringMethod を .ToShortDateString に変更しても。私はまだ同じ問題を抱えています。
[Test()]
public void ValidInEnglish()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = Utility.DatePattern;
DateTime? currentDate = new DateTime(2009,02,7);
string expected = "2009/02/07";
string actual = DateUtils.GetDateInString(currentDate);
//This works
Assert.AreEqual(expected, actual);
}
[Test()]
public void ValidInFrench()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = Utility.DatePattern;
DateTime? currentDate = new DateTime(2009, 02, 7);
string expected = "2009/02/07";
string actual = DateUtils.GetDateInString(currentDate);
// This doesn't work
Assert.AreEqual(expected, actual);
}
public static string GetDateInString(DateTime? obj)
{
if (obj == null || !obj.HasValue)
{
return string.Empty;
}
return obj.Value.ToString(Utility.DatePattern);
}
public const string DatePattern = "yyyy/MM/dd";