I'm trying to format the date and time on my app based on the users set culture info, however, every help resource I see keeps suggesting that I have to manually enter each culture locale in code. For example, if I wanted en-UK I would have to manually add new CultureInfo("en-UK"); with something like new CultureInfo("en-UK");.
Is there a way to just tap into the currently set culture on the phone without me having to actually type the rtc culture info in? Something that might work like "date = ConvertToLocalCultureFormat(date);"?
I don't know if this works on WinPhone7, but you could use
CultureInfo.CurrentCulture.Name
which return the name of the CurrentCulture of the current thread (en-UK or whatever your app runs in)
See for refs
However this should not be necessary.
If you convert your datetime to a string in this way:
DateTime dt = DateTime.Now;
// Converts dt, formatted using the ShortDatePattern
// and the CurrentThread.CurrentCulture.
string dateInString = dt.ToString("d");
you should get the conversion in the right CultureInfo of your phone.
To format anything using the current culture, you don't have to do anything special at all. The overloads of all formatting that doesn't include a specific format or culture uses the default culture.
The Date.ToString() method for example will call the overload with this.ToString(CultureInfo.CurrentCulture) to pick up the current culture setting of the application and use for the formatting.
Which help resources did you read that suggest you have to manually specify the current culture?
The DateTime.ToString() parameterless method automatically uses formatting information derived from the current culture.
This method uses formatting information derived from the current culture. In particular, it combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo object returned by the Thread.CurrentThread.CurrentCulture.DateTimeFormat property.
DateTime exampleDate = new DateTime(2008, 5, 1, 18, 32, 6);
string s = exampleDate.ToString();
// Gives "5/1/2008 6:32:06 PM" when the current culture is en-US.
// Gives "01/05/2008 18:32:06" when the current culture is fr-FR.
// Gives "2008/05/01 18:32:06" when the current culture is ja-JP.
Related
I am trying to change the default date format by the way below but the format wont change and there is no errors
I am excepting to get date in Arabic format. What I am getting now is the default system format date like this (12:00:00 03/01/2011) while I want to overwrite the system format date
Note: Working under oracle11g
TBNextDate.Text = Convert.ToDateTime(oraReder[4])
.ToString("hh:mm:ss dd/MM/yyyy", new CultureInfo("ar-AE"));
According to MSDN:
The provider parameter defines the pattern that corresponds to the standard format specifiers, as well as the symbols and names of date and time components.
When you specify a specific format like you do that does not include any name specifiers, the only thing the culture changes is the separator symbol (e.g. / or .)
If you want to use a date pattern specific to that culture, use a standard format string instead:
TBNextDate.Text = Convert.ToDateTime(oraReder[4]).ToString("d", new CultureInfo("ar-AE"));
I was trying to get the number format in order to convert from string to decimal using the current culture, I have the next configuration in my system:
I would like use the current culture info, but using the customize format like the decimal symbol, digit grouping symbol, etc.
Is it there a way to recover the customize format configuration?
Windows allows users to override the standard property values of the
CultureInfo object and its associated objects by using Regional and
Language Options in Control Panel. The CultureInfo object returned by
the CurrentCulture property reflects these user overrides in the
following cases:
If the current thread culture is set implicitly by the Windows
GetUserDefaultLocaleName function.
If the current thread culture
defined by the DefaultThreadCurrentCulture property corresponds to
the current Windows system culture.
If the current thread culture is
set explicitly to a culture returned by the CreateSpecificCulture
method, and that culture corresponds to the current Windows system
culture.
If the current thread culture is set explicitly to a culture
instantiated by the CultureInfo(String) constructor, and that culture
corresponds to the current Windows system culture.
When you pass CurrentCulture, it will be used to pass the string to a decimal.
decimal number;
if (!decimal.TryParse(numberString, NumberStyles.Number, CultureInfo.CurrentCulture, out number))
{
// Something wrong...
}
You can take current Culture Number Format from the CurrentThread.
var format = Thread.CurrentThread.CurrentCulture.NumberFormat;
EDIT:
CultureInfo.NumberFormat
The user might choose to override some of the values associated with
the current culture of Windows through the regional and language
options portion of Control Panel. For example, the user might choose
to display the date in a different format or to use a currency other
than the default for the culture.
If UseUserOverride is true and the
specified culture matches the current culture of Windows, the
CultureInfo uses those overrides, including user settings for the
properties of the DateTimeFormatInfo instance returned by the
DateTimeFormat property, and the properties of the NumberFormatInfo
instance returned by the NumberFormat property. If the user settings
are incompatible with the culture associated with the CultureInfo, for
example, if the selected calendar is not one of the OptionalCalendars,
the results of the methods and the values of the properties are
undefined.
So var decimalDigits = format.NumberDecimalDigits; should return your custom changes.
I'm parsing a date string from a database so that I can display it in the current culture of the UI thread. For some reason, the date is not parsing with respect to the culture - specifically, I'm parsing a en-US date to switch to a es-ES date and the month/day positions are not switching.
According to this MSDN article I should just be able to use Parse with only the date string as a parameter. I should also be able to explicitly provide a culture object. Neither works and my date remains as mm/dd instead of dd/mm. I've verified that both the thread's CurrentCulture and CurrentUICulture are set properly and if I do a new DateTime, that outputs correctly.
Am I missing something?
EDIT: Nothing fancy in the code, just the .NET API.
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
DateTime formattedDate = DateTime.Parse("5/9/2014");
formattedDate.ToShortDateString(); //this returns 5/9/2014
DateTime.Today.ToShortDateString(); //this returns 9/5/2014
The problem you are having is that 5/9/2014 is a perfectly valid month string in either dd/mm/yyyy or mm/dd/yyyy format so when you do DateTime.Parse("5/9/2014") it will successfully parse it as 5th September 2014 (since the es-es date format is dd/mm/yyyy).
This then explains why when you output you get something different to DateTime.Today (which is obviously 9th May).
A working version of your program would be:
var outputCulture = CultureInfo.CreateSpecificCulture("es-es");
var inputCulture = CultureInfo.CreateSpecificCulture("en-us");
Thread.CurrentThread.CurrentCulture = outputCulture;
Thread.CurrentThread.CurrentUICulture = outputCulture;
DateTime formattedDate = DateTime.Parse("5/9/2014", inputCulture);
Console.WriteLine(formattedDate.ToShortDateString()); //this returns 09/05/2014
Console.WriteLine(DateTime.Today.ToShortDateString()); //this returns 09/05/2014
As you see I am specifying the culture for input so that it knows to use the en-us culture rather than the explicitly set es-es culture for parsing.
Since you are parsing a string from a database, the only way to do this correctly is to persist the string in a standard format that does not depend on any culture specific data. ISO 8601 defines formats appropriate for this and you can use a custom format string to achieve this. You can also use .Net's 'o' format specifier for round trip. See How to: Round-trip Date and Time Values for more information.
Culture specific settings do change and cause items that used to parse, to no longer be able to parse even if you know the culture that was used to format the value with to start with.
Culture specific formatting and parsing is meant to be ephemeral and to be used to interact with the user only.
I have a datetime in this format "Wednesday, December 04, 2013". I want to translate it to different cultures at runtime so that i am able to store that in database according to culture.
This is my code:
dysMngmt.Day = curntDate.ToString("D");
The one line code above is getting the day.
So,please help me.
You can use the second argument of the ToString function, which enables you to pick a culture you see fit:
curntDate.ToString("D", CultureInfo.GetCultureInfo("en-US"))
As a side note, why are you saving the date in your database as a string? Why not use a native date date type? It will take less space and allow you comparisons etc., and then you'd just use the currect culture when reading it out of the database.
Unless you have a very good reason for handling the culture of each date seperatly within the application you should set this at the application level so that the default ToString() works with your intended culture.
http://support.microsoft.com/kb/306162
Also, you should probably also not store dates as text in your database.
I want to display some rows of data on a web page where one column is a DateTime.
I want the date format to be displayed based on the current thread culture.
Right now, I'm doing this (dt is a DateTime):
string s = dt.ToString(Thread.CurrentThread.CurrentCulture.DateTimeFormat);
It's working well, however, on some culture, months and days are represented as only one digit (hours too), for example:
8/8/2011 8:57:59 AM
I would like the date to be displayed like this:
08/08/2011 08:57:59 AM
It would be easier to read (and prettier) when there's a list of rows.
I saw that there's a String.format method I could use, but that makes the current culture irrelevant.
Is there a way to achieve what I'm trying to do?
The solution provided here might be useful.
I see only a single solution - you should obtain the current culture display format, patch it so that it meets your requirement and finally format your DateTime value using the patched format string.
Make a custom culture.
Base it on the current thread culture.
Modify the settings you want to override.
Then either set it back into the thread as the culture or use it temporarily during the format operation.
We currently do this to format all dates in an internationally unambiguous form ddMMMyyyy where MMM is only English three-letter abbreviations, yet obey local numeric formatting rules ./, etc.
The relevant properties to override would be here.
If you want to show it based on the current culture, then what is the problem? If you want a specific format, you have to specify that.
string text = myDateTime.ToString("{0:[your format]}");
I believe this defaults to the server format - but what if you try specifying "u" as the format code which will put the year first then I think two digits.
You can use
String.Format("{0:dd/MM/yyyy hh:MM PM ", yourDatetime)
The date separator / (slash) and time sepatator : (colon) will be rewritten to characters defined in the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator.
EDIT: Forgot to add object param needed to the string.format
using System.Globalization;
private static CultureInfo defaultCulture = new CultureInfo("nl-NL");
public static CultureInfo GetCurrentCulture()
{
List<CultureInfo> badCultures = new List<CultureInfo>();
badCultures.Add(new CultureInfo("en-US"));
if (badCultures.Contains(System.Threading.Thread.CurrentThread.CurrentCulture))
return defaultCulture;
return System.Threading.Thread.CurrentThread.CurrentCulture;
}