簡體   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