繁体   English   中英

C#更改系统日期时间格式

[英]C# change system date time format

我正在寻找一种将系统日期时间格式从12小时更改为24小时的方法。 假设当前,我的系统时间为12或24小时制,我想通过编程将其更改为24小时制。 这怎么可能 ?

谢谢

您可以将SetLocaleInfo API函数与作为LCType参数传递的LOCALE_ITIME常数一起使用。

编辑:

这是设置12小时制的示例。

using System;
using System.Runtime.InteropServices;

namespace DllImportTest
{
    internal static class Program
    {
        private const string TimeFormat12Hour = "0";
        private const string TimeFormat24Hour = "1";

        public static void Main(string[] args)
        {
            var ok = WindowsApi.SetLocaleInfo(0, WindowsApi.LOCALE_ITIME, TimeFormat12Hour);
            if (!ok)
                throw new ApplicationException(string.Format("Windows API call error {0}.", Marshal.GetLastWin32Error()));
        }
    }

    internal static class WindowsApi
    {
        public const int LOCALE_ITIME = 0x00000023;

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool SetLocaleInfo(uint locale, uint lcType, string lcData);
    }
}

请注意, SetLocalInfo文档指出,在对系统参数进行国际更改之后,有必要向所有顶级窗口广播WM_SETTINGCHANGE消息。 为此,需要使用另一个Windows API函数SendMessageTimeout 本示例中省略了此部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM