Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When setting Culture, can I override the date format to e.g. mm/dd/yyyy?

When I set the CurrentCulture, how can I override the date format that is set?

Example:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

Say I wanted to override the default date format so whenever I output DateTime value it is by default in the format I want.

like image 918
loyalflow Avatar asked Mar 20 '23 14:03

loyalflow


1 Answers

First of all you need to create an instance of CultureInfo that won't be read only. Then you can create an instance of your DateTimeFormatInfo, configure it appropriately and finally assign it to your CultureInfo object:

var culture = CultureInfo.CreateSpecificCulture("en-US");
//or simply var culture = new CultureInfo("en-US");
var dateformat = new DateTimeFormatInfo();
//then for example: 
dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";
culture.DateTimeFormat = dateformat;
System.Threading.Thread.CurrentThread.CurrentCulture = culture;

The full specification on how to configure your DateTimeFormatInfo can be found here.

like image 84
Paweł Bejger Avatar answered Mar 23 '23 02:03

Paweł Bejger