大家好,小弟刚学习Delphi不久,处于菜鸟阶段,问一下大家
用Delphi代码如何把年周转换成本周第一天的日期?(以周日为第一天)
如0917(09年17周)转换成20090517(2009年5月17日)

解决方案 »

  1.   

    var
      s:string;
      dt:TDate;
    begin
      dt:=StrToDate('2009-1-1')+(2*7);
      s:=DateToStr(dt);
    end;
      

  2.   


    先取出17这个值
      dt:=StartOfAWeek(2009,17,7);//7表示取周日
    原型
    function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word = 1): TDateTime;
    DescriptionStartOfAWeek returns the first expressible moment (12:00:00.000 AM) of the specified day of the specified week.The AYear parameter specifies the year of the desired day.The AWeekOfYear parameter specifies the week of the year, where 1 is the first week in AYear that includes four or more days.The ADayOfWeek parameter indicates the desired day in the specified week, where 1 is Monday, 2 is Tuesday, and so on.
      

  3.   

    更准确的代码如下:var
      s:string;
      dt:TDate;
      i:Integer;
    begin
      dt:=StrToDate('2009-1-1');
      i:=DayOfWeek(dt);
      self.Label3.Caption:=IntToStr(i);
      dt:=StrToDate('2009-1-1')+(7-i+16*7);//这个就是09年17周第一天
      s:=DateToStr(dt);
      i:=DayOfWeek(dt);
      self.Label1.Caption:=s;
      self.Label2.Caption:=IntToStr(i);
      

  4.   

    上面的代码这句应该改成这样
    dt:=StrToDate('2009-1-1')+(7-i+1+16*7);//这个就是09年17周第一天然后用  DecodeDate(dt1,year,mon,day);
    拆离出年、月、日,组合起来就行了
      

  5.   

    delphi提供的函数已经很好了StartOfAWeek(2009,1,7);返回2009年第一周的周日 2009-01-04
    StartOfAWeek(2009,17,7);返回2009年第17周的周日 2009-04-26