線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5136
推到 Plurk!
推到 Facebook!

如何得知今天是今年的第幾週?

尚未結案
narcysion
一般會員


發表:54
回覆:18
積分:14
註冊:2003-03-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-06-24 13:26:50 IP:203.70.xxx.xxx 未訂閱
請問各位,在delphi中要如何計算現在的日期是今年的第幾週,有什麼元件可引用嗎?
l630521
資深會員


發表:15
回覆:349
積分:372
註冊:2003-02-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-06-24 13:54:25 IP:210.66.xxx.xxx 未訂閱
uses DateUtils; WeekOfTheYear(Now); Ian 發表人 - l630521 於 2004/06/24 13:56:24
narcysion
一般會員


發表:54
回覆:18
積分:14
註冊:2003-03-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-06-24 14:29:21 IP:61.222.xxx.xxx 未訂閱
uses DateUtils; WeekOfTheYear(Now); 上面這個方法在delphi7是可使用的, 但我在delphi5是會出現File not found:'DateUtils.dcu' 請問我在delphi5該如何做,才能出現週數
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-06-24 15:08:43 IP:210.61.xxx.xxx 未訂閱
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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-06-24 15:25:17 IP:140.112.xxx.xxx 未訂閱
土法煉鋼參考一下 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;
系統時間:2024-11-05 5:45:44
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!