怎么样用delphi 写程序修改系统日期

解决方案 »

  1.   

    //Fm: ML 修改系统的日期格式    
      var 
    str: string;beginstr := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];if SetLocaleInfoa(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE , PChar(str)) thenbeginshowmessage('short OK!');thenSendMessageA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);end;end;为了方便测试我用了radioGroup,items 内容如下yy-d-md-yy-mm-d-yyyy-m-dyyyy-m-dyyyy-d-m 传进参数必须 null-terminate 字符串指针。我用 pchar ,而且字符串格式只能写成这样 'm-d'而不是'mm-dd'('yyyy'可以用),不然改不了另外delphi 函数 DateTostr,DateTimeTostr,都调用短格式。长格式很灵活。随便改.RadioGroup Items 如下yyy'年'dd'日'-mmd-yyyy-mmmm-dd-yyyyyyyy-mm-dd代码如下varstr: string;beginstr := RadioGroup2.Items.Strings[RadioGroup2.ItemIndex];if SetLocaleInfoa(LOCALE_SYSTEM_DEFAULT, LOCALE_SLONGDATE , PChar(str)) thenbeginshowmessage('Loan OK!');SendMessageA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);end;end;//你应该搜索一下,有着方面的讨论。 
     
       
      

  2.   

    比如改成yyyy-mm-dd的格式,可以写在data module里,让data module以创建,程序一运行就修改日期SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDATE,'-');
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SSHORTDATE,'yyyy-MM-dd');
      

  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; 
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
       myst:TsystemTime;
    begin
        with MYST do
        begin
        wYear:=strtoint(edit1.Text);
        wMOnth:=strtoint(edit2.text);
        wDay:=strtoint(edit3.Text);
        wHour:=strtoint(edit4.Text);
        wMinute:=strtoint(edit5.Text);
        wSecond:=strtoint(edit6.Text);
        end;
        setsystemtime(myST);end;
      

  5.   

    直接调用API函数:SetLocalTime;SetSystemTime;
      

  6.   

    我怎么在 运行到“setsystemtime(myST);”程序报错
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    VAR
    MYT:_SYSTEMTIME;
    begin
    MYT.wYear:=2001;
    MYT.wMonth:=12;
    MYT.wDay:=1;
    MYT.wHour:=12;
    MYT.wMinute:=2;
    MYT.wSecond:=3;
    SetLocalTime(MYT);
    end;
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
     var
    MYT:Tsystemtime;
    begin
    MYT.wYear:=2001;
    MYT.wMonth:=09;
    MYT.wDay:=01;
    SetlocalTime(MYT);
    end;
    我的程序是这样的,可是能够编译成功,但时间该不了是怎么回事?
      

  9.   

    转贴自超级猛料
    ------------------------------
    设置系统时间在NT/xp中,设置系统时间可能需要特权的,下面的代码可以调整特权来设置时间,和调整关机权限类似。
    http://www.chami.com/tips/delphi/120496D.htmlfunction SetPrivilege(
      sPrivilegeName : string;
      bEnabled : boolean )
        : boolean;
    var
      TPPrev,
      TP         : TTokenPrivileges;
      Token      : THandle;
      dwRetLen   : DWord;
    begin
      Result := False;  OpenProcessToken(
        GetCurrentProcess,
        TOKEN_ADJUST_PRIVILEGES
        or TOKEN_QUERY,
        @Token );  TP.PrivilegeCount := 1;
      if( LookupPrivilegeValue(
            Nil,
            PChar( sPrivilegeName ),
            TP.Privileges[ 0 ].LUID ) )then
      begin
        if( bEnabled )then
        begin
          TP.Privileges[ 0 ].Attributes  :=
            SE_PRIVILEGE_ENABLED;
        end else
        begin
          TP.Privileges[ 0 ].Attributes  :=
            0;
        end;    dwRetLen := 0;
        Result := AdjustTokenPrivileges(
                    Token,
                    False,
                    TP,
                    SizeOf( TPPrev ),
                    TPPrev,
                    dwRetLen );
      end;  CloseHandle( Token );
    end;procedure ChangeSystemTime;
    var
      st : TSystemTime;
    begin
      if( SetPrivilege(
            'SeSystemtimePrivilege',
            True ) )then
      begin
        GetLocalTime( st );    //
        // change time using st structure
        // for example, to 10:30pm
        //
        st.wHour := 22;
        st.wMinute := 30;    SetLocalTime( st );
        // or :
        // SetSystemTime( st );    SetPrivilege(
          'SeSystemtimePrivilege',
          False );
      end;
    end;