.ToShortDateString returns a different cultural format than expected

Here is a strange option.

We have a C # interface that was launched from the beginning of the year without problems on a Windows XP (32-bit) PC. We just upgraded the PC to Windows 7 (64 bit) with the applications installed by SCCM.

At the last launch, dates in the text area began to appear in the US format (5/2/2014), and not in the UK format (02/05/2014).

Used code:

string Lines = FromFormat.Text + " from " + FromFormat.Charge_From.ToShortDateString() + " to " + FromFormat.Charge_To.ToShortDateString() +"."; 

Where FromFormat is an object with raw data, Charge_From and Charge_To are DataTime variables.

We checked the regional settings of the PC and created a small test application to display the PC settings from .Net, both of which are installed in the UK format. Code for the test application:

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

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

Does this have something to do with updating Windows 7? or SCCM?

Thanks in advance

+5
source share
2 answers

After long trials and scratches on our heads, we think we have found the answer to this question.

During testing, we noticed that PCs that had an interface installed through SCCM (only for Windows 7) released formatted text in US format, but those that were directly through Click Once (mainly XP) released text with date formatting In Great Britain.

Further testing confirmed that if we installed a Windows 7 PC through Click, we received text with date formatting in the UK.

After much confusion, we noticed that when SCCM installed the interface, it installed the RTM version of the report viewer, but when Click Once installed the interface, version SP1 was installed to view reports.

We changed SCCM to install Report Viewer SP1 and tested the new version of SCCM and got British dates.

Why the Report Viewer version will affect the culture settings on the PC or how ToShortDateString() works, we have no idea, but this is apparently a problem.

0
source

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

en-GB Culture property ShortDatePattern dd/MM/yyyy .

But the en-US culture is the ShortDatePattern property of M/d/yyyy .

That is why you cannot always replace ToShortDateString and ToString("dd/MM/yyyy") . They are not always the same. And the "/" Specifier of a special format is of particular importance, because I am replaced by the current culture or the specified culture date separator.

I suspect that your regional settings changed during the upgrade process and why the ToShortDateString method generates different results.

But since you did not tell us your CurrentCulture , we never know what the real problem is.

+9
source

Source: https://habr.com/ru/post/1204325/


All Articles