如何得知今天是今年的第幾週? |
尚未結案
|
narcysion
一般會員 發表:54 回覆:18 積分:14 註冊:2003-03-14 發送簡訊給我 |
|
l630521
資深會員 發表:15 回覆:349 積分:372 註冊:2003-02-17 發送簡訊給我 |
|
narcysion
一般會員 發表:54 回覆:18 積分:14 註冊:2003-03-14 發送簡訊給我 |
|
pedro
尊榮會員 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
narcysion您好
從d7 dateutils抄來的, 您貼在使用WeekOfTheYear procedure之前處即可.
procedure DecodeDateWeek(const AValue: TDateTime; out AYear, AWeekOfYear, ADayOfWeek: Word); const CDayMap: array [1..7] of Word = (7, 1, 2, 3, 4, 5, 6); DayMonday = 1; DayTuesday = 2; DayWednesday = 3; DayThursday = 4; DayFriday = 5; DaySaturday = 6; DaySunday = 7; var LDayOfYear: Integer; LMonth, LDay: Word; LStart: TDateTime; LStartDayOfWeek, LEndDayOfWeek: Word; LLeap: Boolean; begin LLeap := DecodeDateFully(AValue, AYear, LMonth, LDay, ADayOfWeek); ADayOfWeek := CDayMap[ADayOfWeek]; LStart := EncodeDate(AYear, 1, 1); LDayOfYear := Trunc(AValue - LStart 1); LStartDayOfWeek := DayOfTheWeek(LStart); if LStartDayOfWeek in [DayFriday, DaySaturday, DaySunday] then Dec(LDayOfYear, 8 - LStartDayOfWeek) else Inc(LDayOfYear, LStartDayOfWeek - 1); if LDayOfYear <= 0 then DecodeDateWeek(LStart - 1, AYear, AWeekOfYear, LDay) else begin AWeekOfYear := LDayOfYear div 7; if LDayOfYear mod 7 <> 0 then Inc(AWeekOfYear); if AWeekOfYear > 52 then begin LEndDayOfWeek := LStartDayOfWeek; if LLeap then begin if LEndDayOfWeek = DaySunday then LEndDayOfWeek := DayMonday else Inc(LEndDayOfWeek); end; if LEndDayOfWeek in [DayMonday, DayTuesday, DayWednesday] then begin Inc(AYear); AWeekOfYear := 1; end; end; end; end; function WeekOfTheYear(const AValue: TDateTime): Word; var LYear, LDOW: Word; begin DecodeDateWeek(AValue, LYear, Result, LDOW); end; |
vinchenkimo
一般會員 發表:0 回覆:37 積分:17 註冊:2004-03-30 發送簡訊給我 |
土法煉鋼參考一下 procedure TForm1.Button1Click(Sender: TObject);
var ADate: TDateTime;
days,weekday,weeks:Integer;
begin
//ADate := StrToDate(Edit1.Text);
adate:=now;
days:=Calcdays(ADate);
weekday:=DayOfWeek(ADate);
weeks:=(days-weekday-1) div 7; // 先扣掉末週天數
weeks:=weeks 1; //加上末週週數
if (days-weekday-1) mod 7>0 then weeks:=weeks 1; //處理首週週數
edit2.text:=inttostr(weeks);
end; function TForm1.Calcdays(ADate:Tdatetime):Integer;
var Year,Month,Day:Word;
i:Integer;
begin
DecodeDate(ADate, Year, Month, Day);
result:=0;
for i:=1 to month-1 do
case i of
1,3,5,7,8,10,12:result:=result 31;
2: if (Year mod 4 =0) and( (Year mod 100 <>0) or (Year mod 400=0)) then result:=result 29
else result:=result 28;
4,6,9,11: result:=result 30
end;
result:=result day;
end;
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |