.ToShortDateString returning different cultural format than expected

Ton*_*ony 6 .net c# datetime sccm windows-7

Here's a weird one for you.

We've got a c# interface that's been running since the beginning of the year without problem on a windows XP (32bit) PC. We've just upgraded the PC to windows 7 (64bit) with apps installed by SCCM.

With the latest run the dates in the text area have started appearing in US format (5/2/2014) instead of UK format (02/05/2014).

The code that is being used is:

string Lines = FromFormat.Text + " from " + FromFormat.Charge_From.ToShortDateString() + " to " + FromFormat.Charge_To.ToShortDateString() +".";
Run Code Online (Sandbox Code Playgroud)

Where FromFormat is an object with the source data, Charge_From & Charge_To are DataTime variables.

We've checked the PC's regional settings and created a little test app to display the pc's settings from .Net both of these are set as UK formats Code for test app:

label1.Text = DateTime.Now.ToString();
label2.Text = DateTime.Now.ToString("dd MMM yyyy");
label3.Text = DateTime.Now.ToShortDateString();
label4.Text = Thread.CurrentThread.CurrentCulture.EnglishName;
Run Code Online (Sandbox Code Playgroud)

I know that I can replace the ToShortDateString() with a ToString("dd/MM/yyyy") to force the correct format but my question is why is this happening?

Is it something to do with the windows 7 upgrade? or the SCCM?

Thanks in advance

Son*_*nül 12

ToShortDateString method uses ShortDatePattern property which is identical of "d" standard date and time format of your CurrentCulture.

en-GB culture's ShortDatePattern property is dd/MM/yyyy.

But en-US culture's ShortDatePattern property is M/d/yyyy.

That's why you can't always replace with ToShortDateString and ToString("dd/MM/yyyy"). They are not always the same. And "/" Custom Format Specifier has a special meaning as replace me with the current culture or specified culture's date separator.

I suspect your regional settings changed on your upgrade process and that's why ToShortDateString method generates different results.

But since you didn't tell us your CurrentCulture, we never know what the real problem is..


Ton*_*ony 1

经过多次测试和绞尽脑汁,我们认为我们已经找到了这个问题的答案。

\n\n

在测试过程中,我们注意到通过 SCCM 安装了界面的 PC\xe2\x80\x99(仅限 Windows 7)正在生成美国日期格式的文本,而那些直接通过 Click Once(主要是 XP)安装的 PC\xe2\x80\x99 正在生成英国日期格式的文本文本。

\n\n

进一步的测试证实,如果我们通过 Click Once 安装 Windows 7 PC,我们会得到英国日期格式的文本。

\n\n

经过一番混乱之后,我们注意到,当 SCCM 安装界面时,它安装的是 RTM 版本的报表查看器,而当 Click Once 安装界面时,安装的是 SP1 版本的报表查看器。

\n\n

我们更改了 SCCM 以安装 Report Viewer SP1,并测试了新的 SCCM 安装版本的界面并获得了英国日期。

\n\n

为什么报表查看器的版本会影响 PC 的文化设置或ToShortDateString()工作方式,我们不知道,但这似乎就是问题所在。

\n