如何设置系统时间?(100分)

  • 主题发起人 主题发起人 ZJUN2001
  • 开始时间 开始时间
Z

ZJUN2001

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DELPHI中设置系统时间及其格式?
 
var
systime: TSystemTime;
begin
systime.wyear := ...;
systime.wmonth := ...;
systime.wday := ...;
....
setsystemtime(systime);
end;
 
格式:Shorttimeformat和LongTimeFormat
 
日期函数Now()、Date()、Time()大家都用得多了,这些函数是读取系统日期时间的。可是遇到需要改变操作系统的时间时,他们就一点办法也没有,而Delphi4又没有提供相关的函数,所以只好求助于API函数SetSystemTime(SystemTime);无奈他要求的变量SystemTime太古怪了,属于TSystemTime,需要经过转换才可以得到,所以举例如下:
  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;
 
不是BUG,而是时差问题:
procedure setSystemDateTime;
var dt: TSYSTEMTIME;
yyyy, mm, dd, hh, _hh, nn, ss, ms: word;
begin
// dtpDate: TDateTimePicker;
DeCodeDate(dtpDate.Date, yyyy, mm, dd);
DeCodeTime(dtpTime.Time, hh, nn, ss, ms);
if hh < 8 then _hh := 16 + hh
else _hh := hh - 8;
dt.wYear := yyyy;
dt.wMonth := mm;
dt.wDay := dd;
dt.wHour := _hh;
dt.wMinute := nn;
dt.wSecond := ss;
dt.wMilliseconds := ms;

try
setSystemTime(dt);
Application.MessageBox('系统时间设置成功!', '设置', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
except
Application.MessageBox('设置系统时间出错!', '错误', MB_OK+MB_ICONHAND+MB_DEFBUTTON1+MB_APPLMODAL);
end;
end;
 
在看到此标题时我突然想到一个问题,在此想请教各位大侠,
如果我想在label中显示系统时间,那我应该怎么做,请教了!
我用
GetLocalTime(SystemTime);//读取北京东8区时间

DateTime:=SystemTimeToDateTime(SystemTime);

label2.caption:=DateTimeToStr(DateTime);
但只能显示一瞬间的系统时间,而我要的是显示是在动的系统时间,如像时钟形式的,
请教各位大侠,我该怎么做,谢了。
 
SetLocalTime();
to 阿阿忠
放在Timer里面不就行了?
 
bubble 谢了,小弟明的了
 
unit DateTime;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
MDateTime = class(TComponent)
private
{ Private declarations }
FDay:word;
FMon:word;
FYear:word;
FHour:word;
FMin:word;
FSec:word;
FMSec:word;
procedure SetDay(aDay:word);
procedure SetMonth(aMon:word);
procedure SetYear(aYear:word);
procedure SetHour(aHour:word);
procedure SetMin(aMin:word);
procedure SetSec(aSec:word);
procedure SetMSec(aMSec:word);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure GetSysDate;
procedure GetSysTime;
procedure GetSysDateTime;
function SetSysDate:boolean;
function SetSysTime:boolean;
function SetSysDateTime:boolean;
procedure SetDate(pDate:TDateTime);
procedure SetTime(pTime:TDateTime);
procedure SetDateTime(pDateTime:TDateTime);
published
{ Published declarations }
Property Day:word read FDay write SetDay;
Property Month:word read FMon write SetMonth;
Property Year:word read FYear write SetYear;
Property Hour:word read FHour write SetHour;
Property Minute:word read FMin write SetMin;
Property Sec:word read FSec write SetSec;
Property MSec:word read FMSec write SetMsec;
end;

procedure Register;

implementation

constructor MDateTime.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
GetSysDateTime;
end;

procedure MDateTime.SetDay;
begin
if ((fMon in [1, 3, 5, 7, 8, 10, 12]) and (aDay in [1..31]))
then fDay:=aDay;
if ((fMon in [4, 6, 9, 11]) and (aDay in [1..30]))
then fDay:=aDay;
if fMon = 2 then begin
if aDay in [1..28] then fDay:=aDay else begin
if (((fYear mod 400 = 0) or ((fYear mod 4 = 0) and (fYear mod 100 > 0))) and (aDay=29))
then fDay:=aDay;
end;
end;
end;

procedure MDateTime.SetMonth;
begin
if ((aMon=2) and (fDay>28)) then fDay:=28;
if aMon in [1..12] then fMon:=aMon;

end;

procedure MDateTime.SetYear;
begin
if ((aYear>1) and (aYear<9999)) then fYear:=aYear;
end;

procedure MDateTime.SetHour;
begin
if (aHour in [0..23])
then fHour:=aHour;
end;

procedure MDateTime.SetMin;
begin
if aMin in [0..59] then fMin:=aMin;
end;

procedure MDateTime.SetSec;
begin
if aSec in [0..59] then fSec:=aSec;
end;

procedure MDateTime.SetMSec;
begin
if (aMSec <= 999) then fMSec:=aMSec;
end;

procedure MDateTime.GetSysDate;
begin
DecodeDate(Date, FYear, FMon, FDay);
end;

procedure MDateTime.GetSysTime;
begin
DecodeTime(Time, FHour, FMin, FSec, FMSec);
end;

procedure MDateTime.GetSysDateTime;
begin
GetSysDate;
GetSysTime;
end;

function MDateTime.SetSysDate:boolean;
var
tSetDati,tDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tDati:=EncodeDate(FYear, FMon, FDay) + Time;
tSetDati := tDati + 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;
SetSysDate := SetSystemTime(tST);
end;

function MDateTime.SetSysTime:boolean;
var
tSetDati,tDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tDati:=Date + EncodeTime(FHour, FMin, FSec, FMsec);
tSetDati := tDati + 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;
SetSysTime := SetSystemTime(tST);
end;

function MDateTime.SetSysDateTime:boolean;
begin
SetSysDateTime:= SetSysDate and SetSysTime;
end;

procedure MDateTime.SetDate(pDate:TDateTime);
begin
DecodeDate(pDate, FYear, FMon, FDay);
end;

procedure MDateTime.SetTime(pTime:TDateTime);
begin
DecodeTime(pTime, FHour, FMin, FSec, FMSec);
end;

procedure MDateTime.SetDateTime(pDateTime:TDateTime);
begin
SetDate(pDateTime);
SetTime(pDateTime);
end;

procedure Register;
begin
RegisterComponents('Milka', [MDateTime]);
end;

end.
 
用美国提子的回答
Shorttimeformat:='yyyy-mm-dd'
 
后退
顶部