[DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]     
public static extern int GetSystemDefaultLCID();     
[DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]     
public static extern int SetLocaleInfo(int Locale, int dateType, string dateData);     
public const int LOCALE_SLONGDATE = 0x20;     
public const int LOCALE_SSHORTDATE = 0x1F;
public const int LOCALE_IDATE = 0x21;       
public const int LOCALE_ILDATE = 0x22; 
这是我设置系统时间的代码 上部分为网上找的(那2个设置长日期,短日期) 可以用
后2句是我查api文档找的(api32) 设置代码如下SetLocaleInfo(i, LOCALE_SSHORTDATE, "yyyy-MM-dd");
SetLocaleInfo(i, LOCALE_SLONGDATE, "yyyy-MM-dd");
SetLocaleInfo(i, LOCALE_IDATE, "HH:mm");
SetLocaleInfo(i, LOCALE_ILDATE, "HH:mm:ss");
多次测试结果表示 我的短日期格式没有设置上去 就是HH:mm系统还是12小时制度 (设置前有获取系统格式 为hh:ss  12小时制,应该为HH 24小时制)是我哪里错了吗? 求大牛..

解决方案 »

  1.   

    换个API试一试下面的代码来自
    http://www.geekpedia.com/code117_Get-And-Set-The-System-Date-And-Time.html
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
     
    namespace Sample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            public struct SystemTime
            {
                public ushort Year;
                public ushort Month;
                public ushort DayOfWeek;
                public ushort Day;
                public ushort Hour;
                public ushort Minute;
                public ushort Second;
                public ushort Millisecond;
            };
     
            [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
            public extern static void Win32GetSystemTime(ref SystemTime sysTime);
     
            [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
            public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
     
            private void button1_Click(object sender, EventArgs e)
            {
                // Set system date and time
                SystemTime updatedTime = new SystemTime();
                updatedTime.Year = (ushort)2008;
                updatedTime.Month = (ushort)4;
                updatedTime.Day = (ushort)23;
                // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
                updatedTime.Hour = (ushort)10;
                updatedTime.Minute = (ushort)0;
                updatedTime.Second = (ushort)0;
                // Call the unmanaged function that sets the new date and time instantly
                Win32SetSystemTime(ref updatedTime);
     
                // Retrieve the current system date and time
                SystemTime currTime = new SystemTime();
                Win32GetSystemTime(ref currTime);
                // You can now use the struct to retrieve the date and time
                MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");
            }
        }
    }
      

  2.   

    具体意思没怎么听懂不过DataTime类的对象有个GetDateTimeFormats()方法。可以返回各种格式的时间的字符串数组。
    比如结果像是:
    2011/7/23
    2011-7-23
    2011.7.23
    2011-07-23
    2011/07/23
    2011.07.23
    11-7-23
    11/7/23
    11.7.23
    11/07/23
    2011年7月23日
    2011-07-23
    星期六, 2011-07-23
    星期六, 2011年7月23日
    2011年7月23日 19:47
    2011年7月23日 19:47
    2011年7月23日 下午 7:47
    2011年7月23日 下午 07:47
    2011-07-23 19:47
    2011-07-23 19:47
    2011-07-23 下午 7:47
    2011-07-23 下午 07:47
    星期六, 2011-07-23 19:47
    星期六, 2011-07-23 19:47
    星期六, 2011-07-23 下午 7:47
    星期六, 2011-07-23 下午 07:47
    星期六, 2011年7月23日 19:47
    星期六, 2011年7月23日 19:47
    星期六, 2011年7月23日 下午 7:47
    星期六, 2011年7月23日 下午 07:47
    2011年7月23日 19:47:41
    2011年7月23日 19:47:41
    2011年7月23日 下午 7:47:41
    2011年7月23日 下午 07:47:41
    2011-07-23 19:47:41
    2011-07-23 19:47:41
    2011-07-23 下午 7:47:41
    2011-07-23 下午 07:47:41
    星期六, 2011-07-23 19:47:41
    星期六, 2011-07-23 19:47:41
    星期六, 2011-07-23 下午 7:47:41
    星期六, 2011-07-23 下午 07:47:41
    星期六, 2011年7月23日 19:47:41
    星期六, 2011年7月23日 19:47:41
    星期六, 2011年7月23日 下午 7:47:41
    星期六, 2011年7月23日 下午 07:47:41
    2011/7/23 19:47
    2011/7/23 19:47
    2011/7/23 下午 7:47
    2011/7/23 下午 07:47
    2011-7-23 19:47
    2011-7-23 19:47
    2011-7-23 下午 7:47
    2011-7-23 下午 07:47
    2011.7.23 19:47
    2011.7.23 19:47
    2011.7.23 下午 7:47
    2011.7.23 下午 07:47
    2011-07-23 19:47
    2011-07-23 19:47
    2011-07-23 下午 7:47
    2011-07-23 下午 07:47
    2011/07/23 19:47
    2011/07/23 19:47
    2011/07/23 下午 7:47
    2011/07/23 下午 07:47
    2011.07.23 19:47
    2011.07.23 19:47
    2011.07.23 下午 7:47
    2011.07.23 下午 07:47
    11-7-23 19:47
    11-7-23 19:47
    11-7-23 下午 7:47
    11-7-23 下午 07:47
    11/7/23 19:47
    11/7/23 19:47
    11/7/23 下午 7:47
    11/7/23 下午 07:47
    11.7.23 19:47
    11.7.23 19:47
    11.7.23 下午 7:47
    11.7.23 下午 07:47
    11/07/23 19:47
    11/07/23 19:47
    11/07/23 下午 7:47
    11/07/23 下午 07:47
    2011/7/23 19:47:41
    2011/7/23 19:47:41
    2011/7/23 下午 7:47:41
    2011/7/23 下午 07:47:41
    2011-7-23 19:47:41
    2011-7-23 19:47:41
    2011-7-23 下午 7:47:41
    2011-7-23 下午 07:47:41
    2011.7.23 19:47:41
    2011.7.23 19:47:41
    2011.7.23 下午 7:47:41
    2011.7.23 下午 07:47:41
    2011-07-23 19:47:41
    2011-07-23 19:47:41
    2011-07-23 下午 7:47:41
    2011-07-23 下午 07:47:41
    2011/07/23 19:47:41
    2011/07/23 19:47:41
    2011/07/23 下午 7:47:41
    2011/07/23 下午 07:47:41
    2011.07.23 19:47:41
    2011.07.23 19:47:41
    2011.07.23 下午 7:47:41
    2011.07.23 下午 07:47:41
    11-7-23 19:47:41
    11-7-23 19:47:41
    11-7-23 下午 7:47:41
    11-7-23 下午 07:47:41
    11/7/23 19:47:41
    11/7/23 19:47:41
    11/7/23 下午 7:47:41
    11/7/23 下午 07:47:41
    11.7.23 19:47:41
    11.7.23 19:47:41
    11.7.23 下午 7:47:41
    11.7.23 下午 07:47:41
    11/07/23 19:47:41
    11/07/23 19:47:41
    11/07/23 下午 7:47:41
    11/07/23 下午 07:47:41
    7月23日
    7月23日
    2011-07-23T19:47:41.1891545+08:00
    2011-07-23T19:47:41.1891545+08:00
    Sat, 23 Jul 2011 19:47:41 GMT
    Sat, 23 Jul 2011 19:47:41 GMT
    2011-07-23T19:47:41
    19:47
    19:47
    下午 7:47
    下午 07:47
    19:47:41
    19:47:41
    下午 7:47:41
    下午 07:47:41
    2011-07-23 19:47:41Z
    2011年7月23日 11:47:41
    2011年7月23日 11:47:41
    2011年7月23日 上午 11:47:41
    2011年7月23日 上午 11:47:41
    2011-07-23 11:47:41
    2011-07-23 11:47:41
    2011-07-23 上午 11:47:41
    2011-07-23 上午 11:47:41
    星期六, 2011-07-23 11:47:41
    星期六, 2011-07-23 11:47:41
    星期六, 2011-07-23 上午 11:47:41
    星期六, 2011-07-23 上午 11:47:41
    星期六, 2011年7月23日 11:47:
      

  3.   

    你不用设置系统的在程序里面 格式化 或者转换就行
       看看我博客这篇文章   http://blog.csdn.net/fengyarongaa/article/details/6346862
      

  4.   

    LZ,NET内库中有现成的DateTime,完全能实现你的要求,为什么非还要使用API的函数,你查查MSDN,上面说得很仔细
      

  5.   


    完全实现我的要求?我的要求是修改系统短时间格式   DateTime怎么修改客户系统上的短时间格式?
      

  6.   

    如果只是为了显示,LZ可能绕了弯路,而且强行改变服务器时间并不是好办法,有可能该服务器另一程序正好只需要12小时制的呢? 难道你非得让客户再单独为你的程序买一台服务器?
    跑题了
    public static DateTime ToDateTime(
    string value,
    IFormatProvider provider
    )可以构造不同的区域以及自定义格式输出时间如果考虑在系统中给DateTime定义扩展方法,效率不会比你调用API慢的
      

  7.   

    这么做不还是要用datetime在每处时间需要格式化的地方转换一下
      

  8.   

    select * from [TableName] where [Time] Between '2011-09-11-01 00:00:00' And '2011-09-14 00:00:00';
    时间格式必须是上面这种,你把时间转成这种就行了,[Time].ToString("yyyy-MM-dd-hh 00:00:00");
      

  9.   

    我怎么觉得我描述的很清楚了  但是有的人还要说ToString呢