I'm running a .NET Core app which contains DateTime.Now.ToString(). When on my PC it displays European date format (dd/mm/yy) but when running on a Linux VM located in the US it displays American date format (mm/dd/yy) despite the timezone on the VM being GMT.
How can I make it display only European date format?
I know I can format it manually inside the program, but is there a way to do this globally on the Linux system?
To set a culture for all application threads you can use CultureInfo.DefaultThreadCurrentCulture property, like that
if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
{
culture = CultureInfo.CreateSpecificCulture("...");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
You are checking that current culture is en-US and update the default culture to any EU specific culture, like de-DE or fr-FR for example. You can also refer to MSDN for details
You can use below code :
DateTime.Now.ToString("dd/MM/yy")
It should work.
For more info: https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
Related
I have a localized WinForms app and the default locale is set German (de-DE). I've added 2 new locales (en-GB and en).
To get a particular culture info I do:
var culture = CultureInfo.GetCultureInfo("en-GB");
When the app is installed and the user's current culture is for example, en-US, how should I handle a fallback to match the other locale I have available, ie. en ?
When doing:
var culture = CultureInfo.GetCultureInfo("en");
The culture is not found and it falls back to my default locale.
I'm running into a very strange issue where the "month/day" standard date format as specified on https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx is rendering differently on my local machine than it is on my azure cloud services and websites.
The culture in this case that is rendering differently is "en-AU". For the date of 2017-05-04 it should render as 4 May and on my local machine it does exactly that. On our website (azure cloud service) and our API (azure website) it renders as May 4. The strange part is that if I use the "short date pattern" it renders as 04/05/2017 on both azure/local. So this seems to be specific only to the "month/day" pattern.
I've tried setting
var culture = new CultureInfo("en-AU");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
and the formatting code is
string.Format(new CultureInfo("en-AU"), "Until {0:M} {0:yyyy}", endDate);
I'm wondering if its possible that the version of some culture definition is different in Azure than it is on my local machine? To my knowledge they are both running .net 4.5. I've added log statements in the code so I can confirm that the culture is set correctly on the line that the code runs, but for some reason, it is just outputting a different value in Azure than it does locally.
I have used both "en-AU" and "en-ZA" culture in both local and Azure environments.Unfortunately,I did not face the issue that you have mentioned in your question in both environments.
It seems the date format that you are getting is US format which might be due to the fact that azure data center that you are using to host your application is based in USA and your date is formatted to that culture.Anyway,give a try to format the date like :
var currentCulture = new CultureInfo("en-AU");
var formattedDate = DateTime.Now.ToString("G",currentCulture);
For the South African culture,try the following:
var currentCulture = new CultureInfo("en-ZA");
var formattedCurrency = currency.ToString("C", currentCulture);
//currency = 100000 then formattedCurrency => R 100 000,00
Good luck ..!!!
Working on a large WPF application and having some issues with the DateTime format, it's a huge codebase and many places do not specify the culture.
When the user opens the application on Windows 7 or 8 the datetime format is different (Win 7 uses slashes and Win 8 uses dashes).
I tried setting the Culture to "en-US" in the Application Startup (see below link) but it doesn't seem work?
Setting Culture (en-IN) Globally in WPF App
How do I set my WPF application to not use the culture of the user machine?
Here is what I use in OnStartup. It's different than the SO question you linked to as I use DefaultThreadCurrentCulture as it sets the default for the entire process and any threads launched.
DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture are in .NET 4.5 and later...
var newCulture = new CultureInfo(displayCulture);
// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";
// You cannot use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture if
// you dont have .NET 4.5 or later. Just remove these two lines and pass the culture
// to any new threads you create and set Thread.CurrentThread...
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
This will provide you with what you are looking for, or you can use regex...
DateTime dateTime;
bool validDate = DateTime.TryParseExact(
"04/22/2015",
"MM/dd/yyyy", // or you can use MM.dd.yyyy
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime);
I've encountered several times the same problem in applications we develop:
We want to allow the user to edit/display it's data in his format (date, currency, ...), but we want to display the application in English only (for several reasons, it's a pro, international application, in a domain in which we communicate mostly in English).
There is no problem when we manage the whole application, but most of third-party pro frameworks that I used (Telerik, DevExpress) are using the CurrentCulture to display my data in the correct format AND in the corresponding language.
So, even if I have my computer in English, I have my regional settings set to fr-CH, I will have all third party user controls in French.
I cannot set the CurrentCulture to a specific culture and set the format of my user controls to something else (I would loose my default format) and I can't let the CurrentCulture to be the default one because I would have my third party components in another language.
I tried to build my own culture (CultureAndRegionInfoBuilder), with no success. When I change the language, I still have my application in the user-specific language.
Concrete problem
I'm using a date editor(basic, it has one text input and can popup a calendar). I want to have the date displayed in my OS locale(ch-FR, so 15 january 2013 would be "15.01.2013"), but I don't want that when I display the calendar month/day name appears in french.
What is the correct approach with this?
Store the original CultureInfo for your purposes and try editing CurrentCulture and CurrentUICulture properties of the CurrentThread property in System.Threading.Thread, maybe this will solve your problem.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I resolved my problem by having a custom culture info:
private static void UpdateCultureInfoWithoutLangage()
{
//We initialize a en-US cultureInfo and change all formats + number infor related
CultureInfo cultureInfoEn = new CultureInfo("en-US");
CultureInfo cultureInfoEnClone = (CultureInfo)cultureInfoEn.Clone();
//Setting DateTimeFormat(Without changing translations)
cultureInfoEnClone.DateTimeFormat.FirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
cultureInfoEnClone.DateTimeFormat.FullDateTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
cultureInfoEnClone.DateTimeFormat.LongDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
cultureInfoEnClone.DateTimeFormat.LongTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
cultureInfoEnClone.DateTimeFormat.MonthDayPattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
cultureInfoEnClone.DateTimeFormat.ShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
cultureInfoEnClone.DateTimeFormat.ShortTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
cultureInfoEnClone.DateTimeFormat.TimeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator;
cultureInfoEnClone.DateTimeFormat.YearMonthPattern = CultureInfo.CurrentCulture.DateTimeFormat.YearMonthPattern;
cultureInfoEnClone.NumberFormat = CultureInfo.CurrentCulture.NumberFormat;
Thread.CurrentThread.CurrentCulture = cultureInfoEnClone;
Thread.CurrentThread.CurrentUICulture = cultureInfoEnClone;
Application.CurrentCulture = cultureInfoEnClone;
}
I'm using a localreport object into an asp.net application.
This report is fed by an array of object. So on the render of the report, some properties of the classe are called.
Class ClassForReport
{
string Date
{
get{return _aDate.ToshortDateString();}
}
}
Now the code for rendering and the problem:
//first of all, I change de culture for taking in account the choice of the user
CultureInfo ci = CultureInfo.CreateSpecificCulture(isoLanguageName_);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
//Here, my culture is now: FR-be
MyLocalReport.render(...) // in this method, the property Date above is called. And when debugging I see that the culture is EN !!!
...
//and here, my culture is still Fr-be
So it seems that when the method render is called, it launch a thread and take the culture of the server and not the culture of the process.
The only workarround I see is changing my report to contains a date and then giving a parameter of culture and formating all my date in all my reports to the given culture...
So I realy hope there is a way to tell the report to take the curent culture of the asp thread and not taking some other culture comming from nowhere.
thx in advance
In ".rdlc" Designer on your ReportFile, set on the Report in Language property "=User!Language".
<Report>
...
<Language>=User!Language</Language>
...
</Report>
then your System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
will work on the values in report.Render(...); like dates,etc.
Cheers.