Set Default DateTime Format c#

2020-01-27 04:13发布

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.

6条回答
Luminary・发光体
2楼-- · 2020-01-27 04:31

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.

查看更多
狗以群分
3楼-- · 2020-01-27 04:41

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");
}
查看更多
男人必须洒脱
4楼-- · 2020-01-27 04:42

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.

查看更多
男人必须洒脱
5楼-- · 2020-01-27 04:48

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.

查看更多
爷的心禁止访问
6楼-- · 2020-01-27 04:54

You can write an ExtensionMethod like this:

public static string ToMyString(this DateTime dateTime)
{
  return dateTime.ToString("needed format");
}
查看更多
老娘就宠你
7楼-- · 2020-01-27 04:58

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.

查看更多
登录 后发表回答