更新文化短时间模式格式未进入 DateTime.now

Updating culture short time pattern format not getting in DateTime.now

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern="DD/MM/YYYY";
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern ="hh:mm tt";

我通过这样做覆盖线程中区域性的日期和时间格式,我们将在 DateTime.Now.

中获得给定格式的 DateTime

我能够获得 Date 的首选格式,同样的事情不适用于 Time。

如何使用上述文化线程以首选格式获取时间。

您可能需要创建类型为 'System.Globalization.CultureInfo' 的对象并设置该对象的日期和时间格式规范。

接下来您需要将线程的当前文化设置为该文化。

下面给出了代码供大家参考

private void UpdateCurrentCulture()
    {
        System.Globalization.CultureInfo objCulture = new System.Globalization.CultureInfo("en-US");

        objCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        objCulture.DateTimeFormat.ShortTimePattern = "hh:mm tt";

        System.Threading.Thread.CurrentThread.CurrentCulture = objCulture;
        System.Threading.Thread.CurrentThread.CurrentUICulture = objCulture;

        Console.WriteLine(DateTime.Now.ToShortDateString());
        Console.WriteLine(DateTime.Now.ToShortTimeString());
    }