C# 设置系统日期格式的方法
程序员文章站
2023-12-15 21:09:10
复制代码 代码如下:[dllimport("kernel32.dll", entrypoint = "getsystemdefaultlcid")] ...
复制代码 代码如下:
[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 (exception ex)
{
console.writeline(ex);
}
}