用DELPHI如何修改机器的系统时间呢?不如把‘2005-12-30’时间格式的修改为系统时间呢?

解决方案 »

  1.   

    procedure TForm1.SetDate(Date: TDateTime);
    Var
      D:TSystemTime;
      Year,Month,Day:Word;
    begin
      Try
        DecodeDate(Date,Year,Month,Day);
        GetSystemtime(D);
        D.wYear:=Year;
        D.wMonth:=Month;
        D.wDay:=Day;
        SetSystemTime(D);
      Except
      end;
    end; //SetSystemTime设置的是GMT时间,SetLocalTime设置的才是本地时区时间。
      

  2.   

    设置系统时间 change the system time?{1.} { 
      For Windows 9X/ME/NT/2000/XP:   The SetLocalTime function fails if the calling process does not have 
      the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. 
      Use the AdjustTokenPrivileges function to enable this privilege. 
    } function SetPCSystemTime(dDateTime: TDateTime): Boolean; 
    const 
      SE_SYSTEMTIME_NAME = 'SeSystemtimePrivilege'; 
    var 
      hToken: THandle; 
      ReturnLength: DWORD; 
      tkp, PrevTokenPriv: TTokenPrivileges; 
      luid: TLargeInteger; 
      dSysTime: TSystemTime; 
    begin 
      Result := False; 
      if (Win32Platform = VER_PLATFORM_WIN32_NT) then 
      begin 
        if OpenProcessToken(GetCurrentProcess, 
          TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then 
        begin 
          try 
            if not LookupPrivilegeValue(nil, SE_SYSTEMTIME_NAME, luid) then Exit; 
            tkp.PrivilegeCount := 1; 
            tkp.Privileges[0].luid := luid; 
            tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; 
            if not AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTOKENPRIVILEGES), 
              PrevTokenPriv, ReturnLength) then 
              Exit; 
            if (GetLastError <> ERROR_SUCCESS) then 
            begin 
              raise Exception.Create(SysErrorMessage(GetLastError)); 
              Exit; 
            end; 
          finally 
            CloseHandle(hToken); 
          end; 
        end; 
      end; 
      DateTimeToSystemTime(dDateTime, dSysTime); 
      Result := Windows.SetLocalTime(dSysTime); 
    end; {************************************************************} {2.} procedure TForm1.Button1Click(Sender: TObject); 
    var 
      SystemTime: TSystemTime; 
      NewTime, NewDate: string; 
    begin 
      NewTime := '13:58:00'; 
      NewDate := '02.02.2001'; // or '02/02/01' 
      DateTimeToSystemTime(StrToDate(NewDate) + StrToTime(NewTime), SystemTime); 
      SetLocalTime(SystemTime); 
      // Tell windows, that the Time changed! 
      PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0); // * 
    end; { 
     Windows 2000 and later: An application should not broadcast 
     the WM_TIMECHANGE message because the system will broadcast 
     this message when the application changes the system time. 
    } {************************************************************} {3.} function SetSystemTime(DateTime: TDateTime): Boolean; 
    { (c) by UNDO } 
    var 
      tSetDati: TDateTime; 
      vDatiBias: Variant; 
      tTZI: TTimeZoneInformation; 
      tST: TSystemTime; 
    begin 
      GetTimeZoneInformation(tTZI); 
      vDatiBias := tTZI.Bias / 1440; 
      tSetDati := DateTime + vDatiBias; 
      with tST do 
      begin 
        wYear := StrToInt(FormatDateTime('yyyy', tSetDati)); 
        wMonth := StrToInt(FormatDateTime('mm', tSetDati)); 
        wDay := StrToInt(FormatDateTime('dd', tSetDati)); 
        wHour := StrToInt(FormatDateTime('hh', tSetDati)); 
        wMinute := StrToInt(FormatDateTime('nn', tSetDati)); 
        wSecond := StrToInt(FormatDateTime('ss', tSetDati)); 
        wMilliseconds := 0; 
      end; 
      Result := Windows.SetSystemTime(tST); 
    end;
      

  3.   

    Winexec('cmd /c time 20:20:20',SW_HIDE);
      

  4.   

    Winexec('cmd /c date 1988-8-8',SW_HIDE);
      

  5.   

    SetSystemTime注意时区即可
    基础东西,baidu随时有的
      

  6.   

    昨天刚看到的,没来得及试。改变系统时间
      1、定义变量
    var SystemTime: TSystemTime;
      2、转换日期
    DateTimeToSystemTime(StrToDatetime('1999-09-01 11:12:12' ),SystemTime);
      3、改变系统日期
    SetSystemTime(SystemTime);
      到此系统日期已经改变,可是由于API函数SetSystemTime()本身存在的BUG,
    在你改变系统日期以后,等待一会,你会看到系统的日期是对的,可是时间却错了,
    并不是我们设定的11:12:12,这样的问题看来需要微软才能解决了/////////////////////       方法二           /////////////////////////
    { SetDate sets the current date in the operating system. Valid  }
    { parameter ranges are: Year 1980-2099, Month 1-12 and Day      }
    { 1-31. If the date is not valid, the function call is ignored. }
    procedure SetDate(Year, Month, Day: Word); assembler;
    asm
     MOV CX,Year
     MOV DH,BYTE PTR Month
     MOV DL,BYTE PTR Day
     MOV AH,2BH
     INT 21H
    end;{ SetTime sets the time in the operating system. Valid          }
    { parameter ranges are: Hour 0-23, Minute 0-59, Second 0-59 and }
    { Sec100 (hundredths of seconds) 0-99. If the time is not       }
    { valid, the function call is ignored.                          }
    procedure SetTime(Hour, Minute, Second, Sec100: Word); assembler;
    asm
     MOV CH,BYTE PTR Hour
     MOV CL,BYTE PTR Minute
     MOV DH,BYTE PTR Second
     MOV DL,BYTE PTR Sec100
     MOV AH,2DH
     INT 21H
    end;function SetSystemDateTime(Year, Month, Day, Hour, Minute, Second: word): integer;   export;
    begin
      SetDate(Year, Month, Day);
      SetTime(Hour, Minute + 1, Second, 0);
      result := 1;
    end;