Setting own culture or changing the current culture for datetime - c#

I'm trying to set custom culture in my project. But I have some problem I've searched Google and found the following code. But I have some problems with it, please observe it in the comment.
using System;
using System.IO;
using System.Globalization;
public class Example
{
public static void Main()
{
// Persist the date and time data.
StreamWriter sw = new StreamWriter(#".\DateData.dat");
// Create a DateTime value.
DateTime dtIn = DateTime.Now;
// Retrieve a CultureInfo object.
CultureInfo invC = CultureInfo.InvariantCulture;
// Convert the date to a string and write it to a file.
sw.WriteLine(dtIn.ToString("r", invC));//what r mean?. if r is the custem culture variabel then how we determin it.
sw.Close();
// Restore the date and time data.
StreamReader sr = new StreamReader(#".\DateData.dat");
String input;
while ((input = sr.ReadLine()) != null)
{
Console.WriteLine("Stored data: {0}\n" , input);
// Parse the stored string.
DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);
// Create a French (France) CultureInfo object.
CultureInfo frFr = new CultureInfo("fr-FR");
// Displays the date formatted for the "fr-FR" culture.
Console.WriteLine("Date formatted for the {0} culture: {1}" ,
frFr.Name, dtOut.ToString("f", frFr));// f?
// Creates a German (Germany) CultureInfo object.
CultureInfo deDe= new CultureInfo("de-De");
// Displays the date formatted for the "de-DE" culture.
Console.WriteLine("Date formatted for {0} culture: {1}" ,
deDe.Name, dtOut.ToString("f", deDe));
}
sr.Close();
}
}

Here's a link that shows many formatting values for the DateTime.ToString() method. I see no lower case "r" mentioned but the output of your code seems be the same with "R" or "r".
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
The DateTime value that you are writing to the file would be based on the invariant culture before any culture changes. You write it out and the you read it back in before getting some new culture information.
I had to guess at what you were asking because there is no question anywhere but in the code. Please provide more detail if I misunderstood what you are asking about.
Maybe if you were to show your output, it would help.
Ah, and here's a link that actually says that "r" is the same as "R". So now you have documentation for that part of your question:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

Related

Convert(change) current DateTime as per culture in c#

if (!IsPostBack && !Page.IsCallback)
{
double OffsetHrs = GetTimeZoneOffsetFromCookie();
string dateFormat = ServiceManager.LocalizationService.GetString("AppHeaderTop", "DateFormat", "g");
CultureSelected CultureSelected = GetCultureSelected();
ASPxLabelCurrentTime.Text = DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat);
if (CultureSelected.CultureCode != "en-US")
{
DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo currentDtfi = new CultureInfo(CultureSelected.CultureCode, false).DateTimeFormat;
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi).ToString(currentDtfi.ShortDatePattern); //what can i Use here ?
}
Let say Output of ASPxLabelCurrentTime.Text
for en-US culture is 11/2/2015 4:14 PM (70)
If I select specific culture I want this datetime 11/2/2015 4:14 PM (70) to appear in that specific culture format.
Your question seems unclear but I try to give a shot.
First of all, what is this (70) exactly? Where is this came from? en-US culture can't parse this string without using it in a string literal delimiter with ParseExact or TryParseExact methods. On the other hand, since you assing ASPxLabelCurrentTime.Text the result of the DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat) code, I don't believe this (70) part is really an issue on this question.
Second, If I understand clearly, the problem seems the usage of DateTime.ToString(string) method.
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
.ToString(currentDtfi.ShortDatePattern);
// ^^^ Problem seems here
Okey let's say you successfully parse this ASPxLabelCurrentTime.Text with usDtfi culture (which is en-US), but with this .ToString(string) method, you are not using currentDtfi settings actually, you are using CurrentCulture settings when you generate formatted string representation of your DateTime.
From DateTime.ToString(String) doc;
Converts the value of the current DateTime object to its equivalent
string representation using the specified format and the formatting
conventions of the current culture.
Since we don't know what GetCultureSelected method returns exactly, it may or may not be the same culture with currentDtfi.
I strongly suspect, you can solve this problem to using that culture as a second parameter in ToString method as;
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
.ToString(currentDtfi.ShortDatePattern, currentDtfi);
IF this (70) is really part of on your string, you need to ParseExact or TryParseExact methods to supply exact format of it.
string s = "11/2/2015 4:14 PM (70)";
DateTime dt;
if(DateTime.TryParseExact(s, "MM/d/yyyy h:mm tt '(70)'", CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.None, out dt))
{
ASPxLabelCurrentTime.Text = dt.ToString(currentDtfi.ShortDatePattern, currentDtfi);
}

.NET - Canadian DateTime Format Bug

I currently have an application that needs to support multiple cultures. A date string is being passed in and is being parsed to a DateTime object. To allow for multiple cultures I am first setting the thread's culture and UICulture.
Here is an example of my code as it is:
First I set the culture for this thread. This is passed in on the URL (ex. en-us, en-ca, fr-ca):
CultureInfo ci = new CultureInfo(culture, false);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;
I then take a date string that is also passed in on the URL and convert it to a DateTime. Depending on the culture the date will be passed in as mm/dd/yyyy or dd/mm/yyyy.
I parse the date strings with the following code:
DateTime DateTimeObject;
bool validDate = DateTime.TryParse(DateStringFromURL, Thread.CurrentThread.CurrentCulture, DateTimeStyles.None, out DateTimeObject);
The TryParse works for most cultures and out comes a valid datetime object. A problem arises with en-ca and fr-ca though. If the wrong format is passed in, the datestring does not parse correctly.
The following shows which format .NET seems to expect for each culture:
dd/MM/yyyy MM/dd/yyyy
EN-CA Valid Invalid
FR-CA Invalid Valid
EDIT:
To be more exact here are the examples causing me problems:
For EN-CA:
DateTime.Parse("26/08/2014") //VALID
DateTime.Parse("08/26/2014") //EXCEPTION THROWN
For FR-CA:
DateTime.Parse("26/08/2014") //EXCEPTION THROWN
DateTime.Parse("08/26/2014") //VALID
This is backwards from how other systems treat this culture's date formatting.
Why are the formats seemingly backwards?
Is this a bug with .NET 4.5?
Any and all help would be appreciated, thanks.
NOTE: .NET Version 4.5
You should probably look into Invariant culture. That is what you should use for your URL passing. You can use the local culture for display purposes on the client.
Typically, whenever you are storing data (for example, in a database or file ) it is best to use the Invariant culture. When displaying data you can use the local culture. When passing data around, you want to use Invariant culture.
Read more here.
I believe your assumptions are wrong. This console application:
static void Main(string[] args)
{
var enca = new CultureInfo("en-ca", false);
var frca = new CultureInfo("fr-ca", false);
Console.WriteLine("enca dd/MM/yyyy: " + ParseDate("26/08/2014", enca));
Console.WriteLine("enca MM/dd/yyyy: " + ParseDate("08/26/2014", enca));
Console.WriteLine("frca dd/MM/yyyy: " + ParseDate("26/08/2014", frca));
Console.WriteLine("frca MM/dd/yyyy: " + ParseDate("08/26/2014", frca));
Console.ReadKey();
}
static bool ParseDate(string date, CultureInfo culture)
{
try
{
DateTime.Parse(date, culture);
return true;
}
catch
{
return false;
}
}
Has output:
enca dd/MM/yyyy: False
enca MM/dd/yyyy: True
frca dd/MM/yyyy: False
frca MM/dd/yyyy: True
There is definitely a bug in data parsing for Canadian culture. If you try following having CurrentCulture = "en-CA":
DateTime.ParseExact("2014/09/20", "yyyy/MM/dd", Thread.CurrentThread.CurrentCulture)
you'll get following exception:
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
works fine if you replace / with something else, like - or with InvariantCulture.

convert DateTime to string according to customCultureInfo in G((combination of date and Time) format

I have changed my system date format to Faeroese.
I want to convert DateTime to String according to customCulture with G format (combination of date and Time)
check the below code.
namespace TestDateConvertion
{
class Program
{
static void Main(string[] args)
{
object value = new DateTime(2003,12,23,6,22,30);
DateTime dateTimeValue = (DateTime)value;
CultureInfo customCulture = MySettings.getCustomCulture();
//for getting custom culture in my app
//in custom culture i have changed shortDateFormat according to the user preference.
//value in shortDateFormat = dd/MM/yyyy
string result = string.Format(customCulture, "{0:G}", result);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
but i get the output with sepertators according to system DateTime not with users given format in customCulture,
i even dont find any method overloaded in string.Format() or DateTime.ToString() to do this.
If i pass CultureInfo.InvariantCulture then i cant get output in G format.
try this:
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("G", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G", CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM
Console.WriteLine(date1.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")));
According to Standard Date and Time Format Strings "G" uses short date format (as you claim to specify). So most likely reason of using local culture separator is covered in The "/" Custom Format Specifier portion of ""Custom Date and Time Format Strings".
Since your "short date format" is "dd/MM/yyyy" than instead of "/" it will use corresponding separator from the culture info (which you are likely picking from default culture).
Escaping with \ is covered in the Using the escape character portion of the same "Custom Date and Time Format Strings" article.
So you want your shortDateFormat = #"dd\/MM\/yyyy" or properly specify DateTimeSeparator in corresponding part of your custom CultureInfo.

Having difficulties converting a string into decimal

Thanks for taking the time to assist me with my problem.
In the code I'm writing, I'm iterating through a table, I get the appropriate values (confirmed it using the debugger) and I'm parsing them to the appropriate types before and finally I add them to an Object to be serialized into XML.
However, I bumped into a problem and that is I can't seem to find a way to parse the string into a decimal value. Take a look:
if (DateTime.TryParse(dateString, culture, styles, out date))
{
decimal LastValue;
string vrednost = String.Format("{0:0,0.0}",
row.SelectSingleNode("td[2]").InnerText);
if (Decimal.TryParse(vrednost, out LastValue))
list.Add(new StockEntry
{
Date = date,
PoslednaCena = LastValue
...
}
Note that the value of vrednost is 4.451,00 and I suspect that if I convert it to 4,451.00 it will get parsed.
I've succeeded in parsing date into the appropriate datetime value. However, the value of LastValue is always 0. I've exhausted all the resources that I know of. Do you have any idea how to solve my problem?
Thank you in advance!
This formatting will do nothing because you can't format strings like this. You have to use parse method with additional parameters and specify your own format
string s2 = "4.451,00";
NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
numberFormatInfo.NumberDecimalSeparator = ",";
numberFormatInfo.NumberGroupSeparator = ".";
var d = decimal.Parse(s2, numberFormatInfo);
I think that your problem might be due to the culture used for the parsing. Try using CultureInfo.InvariantCulture for your parsing. It should work with "," as thousands separator and "." as decimal separator.
Decimal.TryParse(vrednost, NumberStyles.Number, CultureInfo.InvariantCulture, out LastValue);
If you want to swap them you could use another culture. Italian, for instance, works with your format (not sure about the others), so your code for "4.451,00" would look like:
Decimal.TryParse(vrednost, NumberStyles.Number, CultureInfo.GetCultureInfo("it"), out LastValue);
If you want to use a custom culture instead of forcing some culture which does what you want you can simply create your NumberFormatInfo class and pass it to the parse method.
NumberFormatInfo decimalNumber = new NumberFormatInfo();
decimalNumber.NumberDecimalSeparator = ",";
decimalNumber.NumberGroupSeparator = ".";
Decimal.TryParse(vrednost, NumberStyles.Number, decimalNumber, out LastValue);
Your row.SelectSingleNode("td[2]").InnerText is a string and you are trying to format it like a decimal.
Try parsing it directly:
decimal LastValue;
string vrednost = row.SelectSingleNode("td[2]").InnerText;
if (Decimal.TryParse(vrednost, out LastValue))
Check your cultureinfo at first and set it appropriately.
CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("The culture: "+ MyUsersCulture.Name);
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
ConsoleWriteLine("The culture: " + Thread.CurrentThread.CurrentCulture);

Set Default DateTime Format c#

Is there a way of setting or overriding the default DateTime format for an entire application. I am writing an app in C# .Net MVC 1.0 and use alot of generics and reflection. Would be much simpler if I could override the default DateTime.ToString() format to be "dd-MMM-yyyy". I do not want this format to change when the site is run on a different machine.
Edit -
Just to clarify I mean specifically calling the ToString, not some other extension function, this is because of the reflection / generated code. Would be easier to just change the ToString output.
The "default format" of a datetime is:
ShortDatePattern + ' ' + LongTimePattern
at least in the current mono implementation.
This is particularly painful in case you want to display something like 2001-02-03T04:05:06Z i.e. the date and time combined as specified in ISO 8606, but not a big problem in your case:
using System;
using System.Globalization;
using System.Threading;
namespace test {
public static class Program {
public static void Main() {
CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
culture.DateTimeFormat.LongTimePattern = "";
Thread.CurrentThread.CurrentCulture = culture;
Console.WriteLine(DateTime.Now);
}
}
}
This will set the default behavior of ToString on datetimes to return the format you expect.
It is dependent on your application's localization-settings. Change that accordingly to get correct format.
Otherwise have a helper-class or an extension-method which always handles your DateTime.
public static string ToMyDateTime(this DateTime dateTime) {
return dateTime.ToString("dd-MMMM-yy");
}
DateTime.ToString() combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo. You can specify these patterns in DateTimeFormatInfo.CurrentInfo.
I've never tried this my self.
If you want to be sure that your culture stays the same, just set it yourself to avoid troubles.
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("nl-BE");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
The above example sets the culture of the thread to Belgian-Dutch.
CurrentCulture does all the date and time handling and CurrentUICulture handles UI localization like resources.
I'm not sure if this would work for a web app, but you could try to set the DateTimeFormat property for the current culture.
Check this and specially this.
Using .Net 6 put something like this in your program.cs after app.UseAuthentication()/app.UseAuthorization() and before app.MapControllerRoute(...):
var ci = new CultureInfo("en-US");
ci.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture(ci),
SupportedCultures = new List<CultureInfo> { ci },
SupportedUICultures = new List<CultureInfo> { ci }
});
Here I'm changing the short date format, but you can also change currency symbol, decimal separator, etc.
You can write an ExtensionMethod like this:
public static string ToMyString(this DateTime dateTime)
{
return dateTime.ToString("needed format");
}

Categories

Resources