如何将任一时间段分离出来,有点难,请进。。。急(100分)

  • 主题发起人 主题发起人 apower
  • 开始时间 开始时间
A

apower

Unregistered / Unconfirmed
GUEST, unregistred user!
现在要将任一时间段分离出如下格式:
如将85.09-92.08(日期为YY.MM),分离成:
85.09
85.10
85.11
85.12
86.01
86.02
...
92.07
92.08

如何用最简便的方法,请高手们指教。
 
我将分后的字符占时添加到了memo 中,请楼主根据实际情况,看是否可用
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
i,j:integer; //i是年,j 是月
begin
form1.Memo1.Clear;
s:='85.09-92.08';
  i:=85;
begin
for j:=8 to 9 do
begin
form1.Memo1.Lines.Add(inttostr(i)+'.0'+inttostr(j))
end;
for j:=10 to 12 do
begin
form1.Memo1.Lines.Add(inttostr(i)+'.'+inttostr(j))
end;
end;
for i:=86 to 91 do
begin
for j:=1 to 9 do ////// 一位数月份
begin
form1.Memo1.Lines.Add(inttostr(i)+'.0'+inttostr(j))
end;
for j:=10 to 12 do  两位数月份
begin
form1.Memo1.Lines.Add(inttostr(i)+'.'+inttostr(j))
end;
end;
i:=92;
begin
for j:=1 to 9 do
begin
form1.Memo1.Lines.Add(inttostr(i)+'.0'+inttostr(j))
end;
end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
OldSDF: String;
OldDS : Char;
T1, T2: TDate;
begin
OldSDF := ShortDateFormat;
OldDS := DateSeparator;
try
DateSeparator := '.';
ShortDateFormat := 'yy.mm';
T1 := StrToDate(Copy(Edit1.Text,1,5)+'.01');
T2 := StrToDate(Copy(Edit1.Text,7,5)+'.01');

while T1 <= T2 do
begin
Memo1.Lines.Add(FormatDateTime('yy.mm', T1));
T1 := IncMonth(T1);
end;
finally
DateSeparator := OldDS;
ShortDateFormat := OldSDF;
end;
end;
 
formatdatetime();
 
感谢高手tseug,成功了,很简单。但以下语句少了参数
T1 := IncMonth(T1,1);

 
呵呵,是吗...你看看声明

{ IncMonth returns Date shifted by the specified number of months.
NumberOfMonths parameter can be negative, to return a date N months ago.
If the input day of month is greater than the last day of the resulting
month, the day is set to the last day of the resulting month.
Input time of day is copied to the DateTime result. }

function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1): TDateTime;
 
//改进了点,可根据"-"分割两段时间.
var
OldSDF: String;
OldDS : Char;
T1, T2: TDate;
a1,a2 : integer;
begin
OldSDF := ShortDateFormat;
OldDS := DateSeparator;

try
list :=tstringlist.create;
DateSeparator := '.';
ShortDateFormat := 'yy.mm';
a1 := Length(Edit1.text);
a2 := Pos('-',edit1.Text);
T1 := StrToDate(Copy(Edit1.Text,1,a2-1)+'.01');
T2 := StrToDate(Copy(Edit1.Text,a2+1,a1-a2)+'.01');
memo1.Lines.Clear;
while T1 <= T2 do
begin
list.Add(FormatDateTime('yy.mm', T1)) ;
Memo1.Lines.Add(FormatDateTime('yy.mm', T1));
T1 := IncMonth(T1,1);
end;
finally
DateSeparator := OldDS;
ShortDateFormat := OldSDF;
end;
end;
 
yearof()
dayof()
 
datetimetostring好像适合斑竹的要求。不妨在delphi的帮助中看看,
可以设置很多不同的格式。
 
yearof()
dayof()
看看帮助,好多这样的函数的
 
var
OldSDF: String;
OldDS : Char;
T1, T2: TDate;
list:tstringlist;
begin
OldSDF := ShortDateFormat;
OldDS := DateSeparator;
try
list :=tstringlist.create;
list.Delimiter:='-';//以-作分隔符
list.DelimitedText:=edit1.text;
DateSeparator := '.';
ShortDateFormat := 'yy.mm';
T1 := StrToDate(list[0]+'.01');
T2 := StrToDate(list[1]+'.01');
memo1.Lines.Clear;
while T1 <= T2 do begin
Memo1.Lines.Add(FormatDateTime('yy.mm', T1));
T1 := IncMonth(T1,1);
end;
finally
DateSeparator := OldDS;
ShortDateFormat := OldSDF;
list.Free;
end;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部