how to change culture date format in c#? - c#

how to change culture date format in c#

Are you asking for this?
Formatting Date and Time for a Specific Culture

If you need to you can change the culture for the current thread so you don't have to do it for each call.
Thread.CurrentThread.CurrentCulture = New CultureInfo("th-TH", False)
More information here:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture%28VS.71%29.aspx

For example in DateTimePicker you can use CustomFormat property to add the format you want(independent on culture). or see #gcores's answer.

Related

C#: ShortDatePattern giving incorrect result

I have dd-MM-yyyy date pattern for my windows machine. In below code I'm trying to get date format through C# code, but it's giving me M/d/yyy instead of dd-MM-yyyy. In code is correct ?
Console.WriteLine(DateTime.Now);
Console.WriteLine(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern);
Use CurrentCulture instead of CurrentUICulture`.
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
CurrentCulture is for formatting of numbers and dates.
CurrentUICulture for localisation.
Try this:-
You are using CurrentUICulture which is used for localisation instead use CurrentCulture which is used for formatting of numbers and dates
Console.WriteLine(DateTime.Now);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
You can probably use: DateTime.Now.ToString("d");
Documentation about DateTime formatting can be found here: Standard DateTime Format Strings

Formatting DateTime - ignore culture

I need to format a date to the following format:
M-d-yyyy
I tried using:
string.Format("{0:M-d-yyyy}", DateTime.Now)
But the output string will depend on the CurrentCulture on the computer where it's run, so sometimes the output might be 07/09/2014 or 07.09.2014 instead of 09-07-2014.
How can I easily prevent it from converting it based on the culture and treating it as a literal string?
Use CultureInfo.InvariantCulture as the culture or provider argument.
String.Format(CultureInfo.InvariantCulture, "{0:M-d-yyyy}", DateTime.Now)
Use CultureInfo.InvariantCulture as an IFormatProvider parameter:
DateTime.Now.ToString("M-d-yyyy", CultureInfo.InvariantCulture);
You can set the culture of your program with this:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;`
You can also use a specific culture if you want (I think en-US is the one you need)
Use the following:
DateTime.Now.ToString("d", DateTimeFormatInfo.InvariantInfo);
or apply other formatting specs as detailed in http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx
Pertinent to your case it could be written as:
DateTime.Now.ToString("M-d-yyyy", DateTimeFormatInfo.InvariantInfo);
Regards,
You can use the .ToString() method on the DateTime object to format it however you'd like. Your code would look something like this:
DateTime.Now.ToString("M-d-yyyy");
More info on formatting date times can be found on the MSDN: http://msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.110%29.aspx
you can try
date.ToString("MM/dd/yy", yyyymmddFormat);
or
try whats in this link
http://social.msdn.microsoft.com/Forums/en-US/af4f5a1e-f81d-47fe-981d-818e785b8847/convert-string-to-datetime-object
you can force the string into a standard format if you like

DateTime.Parse not reversing month and day based on thread culture

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.

Format a date using the current thread culture Vs double digits for month and day?

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 DateTimeForma­tInfo.DateSepa­rator and DateTimeForma­tInfo.TimeSepa­rator.
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;
}

.NET DateTime not returning AM/PM in ToShortTimeString()

I've run into a problem that's driving me crazy. In my application (ASP.NET MVC2 /.NET4), I simply running this:
DateTime.Now.ToShortTimeString()
All the examples I've seen indicate I should get something like: 12:32 PM, however I'm getting 12:32 without the AM/PM.
I launched LinqPad 4 to see if I could replicate this. Instead, it returns 12:32 PM correctly.
What the hell?
You may also try a custom format to avoid culture specific confusions:
DateTime.Now.ToString("hh:mm tt")
KBrimington looks to be correct:
The string returned by the ToShortTimeString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short time pattern is "h:mm tt"; for the de-DE culture, it is "HH:mm"; for the ja-JP culture, it is "H:mm". The specific format string on a particular computer can also be customized so that it differs from the standard short time format string.
From MSDN
If you don't want to mess with the Culture for your whole thread/application, try this:
CultureInfo ci = new CultureInfo("en-US");
string formatedDate = DateTime.Now.ToString("t", ci);
You can find the list of DateTime Format strings here.
Yeah, this depends on your Locale. What is the value of System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern in your application?
See MSDN Link
You can set the thread's culture info and this will then be used by the ToShortTimeString() method. But understand that this will effect all code running in that thread.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
The function uses the users default patterns. They can be changed in the Control panel. Check out first tab in the 'Region and Language' Settings. Change the Short time pattern to a pattern that like 'h:mm tt' and you're done.
This may also need the CultureInfo.InvariantCulture
e.g. DateTime.Now.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture)

Categories