1. 程式人生 > WINDOWS開發 >Winfrom 設定PC 端日期格式

Winfrom 設定PC 端日期格式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ZB.QueueSys.Common
{
    public class DateHelper
    {
        private static DateHelper instance;
        public static DateHelper Instance
        {
            get
            {
                if (instance == null) instance = new DateHelper();
                return DateHelper.instance;
            }
        }


        [DllImport("kernel32.dll",EntryPoint = "GetSystemDefaultLCID")]
        public static extern int GetSystemDefaultLCID();
        [DllImport("kernel32.dll",EntryPoint = "SetLocaleInfoA")]
        public static extern int SetLocaleInfo(int Locale,int LCType,string lpLCData);
        public const int LOCALE_SLONGDATE = 0x20;
        public const int LOCALE_SSHORTDATE = 0x1F;
        public const int LOCALE_STIME = 0x1003;

        public void SetDateTimeFormat()
        {
            try
            {
                int x = GetSystemDefaultLCID();
                SetLocaleInfo(x,LOCALE_STIME,"HH:mm:ss");        //時間格式
                SetLocaleInfo(x,LOCALE_SSHORTDATE,"yyyy-MM-dd");   //短日期格式  
                SetLocaleInfo(x,LOCALE_SLONGDATE,"yyyy-MM-dd");   //長日期格式 
            }
            catch { return; }
        }

    }
}