如:2006年3月31日--2007年3月31日
 分别算出:第一季度从_年_月_日 到 _年_月_日 (3个月)
      第二季度从_年_月_日 到 _年_月_日
      第三季度从_年_月_日 到 _年_月_日
      第四季度从_年_月_日 到 _年_月_日
 
              第一个月从_年_月_日 到 _年_月_日
                .
                .
                .
              第十二个月从_年_月_日 到 _年_月_日                本月的第一周从_年_月_日 到 _年_月_日
                             .
                             .
                             .
                    本月的第四周(或第五周)从_年_月_日 到 _年_月_日  多谢!

解决方案 »

  1.   

    这个问题不难,DateUtils单元里有很多关于日期的函数,可到帮助文件里查
      

  2.   

    uses DateUtils
    procedure TForm1.Button3Click(Sender: TObject);
    var
        dtBegin,dtEnd,dtTemp:TDateTime;
        iDays,iDays1:integer;
        iCnt,iCnt1:integer;
        iCurWeek,iLastPos:integer;
    begin
        dtBegin:=StrToDateTime(Edit2.Text)+1;
        dtEnd:=StrToDateTime(Edit3.Text);
        dtTemp:=dtBegin;
        iCnt:=1;
        while true do
        begin
            iDays:=DaysInMonth(dtTemp);
            Memo2.Lines.Add('第'+IntToStr(iCnt)+'个月:'+FormatDateTime('yyyy-mm-dd',dtTemp)+'----'+FormatDateTime('yyyy-mm-dd',(dtTemp+iDays-1)));
            iDays1:=1;
            iCurWeek:=WeekOfTheMonth(dtTemp);
            iLastPos:=0;
            iCnt1:=1;
            while true do
            begin
                while(WeekOfTheMonth(dtTemp+iDays1)=iCurWeek)do
                    inc(iDays1);
                iCurWeek:=WeekOfTheMonth(dtTemp+iDays1);
                if MonthOf(dtTemp+iDays1)<>MonthOf(dtTemp) then
                begin
                    Memo2.Lines.Add('    本月第'+IntToStr(iCnt1)+'周:'+FormatDateTime('yyyy-mm-dd',dtTemp+iLastPos)+'----'+FormatDateTime('yyyy-mm-dd',(dtTemp+iDays-1)));
                    break;
                end else
                begin
                    Memo2.Lines.Add('    本月第'+IntToStr(iCnt1)+'周:'+FormatDateTime('yyyy-mm-dd',dtTemp+iLastPos)+'----'+FormatDateTime('yyyy-mm-dd',(dtTemp+iDays1-1)));
                    inc(iCnt1);
                    iLastPos:=iDays1;
                end;
            end;
            inc(iCnt);
            dtTemp:=incMonth(dtTemp);
            if dtTemp>dtEnd then
                break;
        end;end;
      

  3.   

    function StartOfTheYear(const AValue: TDateTime): TDateTime;
    function EndOfTheYear(const AValue: TDateTime): TDateTime;
    function StartOfAYear(const AYear: Word): TDateTime;
    function EndOfAYear(const AYear: Word): TDateTime;function StartOfTheMonth(const AValue: TDateTime): TDateTime;
    function EndOfTheMonth(const AValue: TDateTime): TDateTime;
    function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
    function EndOfAMonth(const AYear, AMonth: Word): TDateTime;function StartOfTheWeek(const AValue: TDateTime): TDateTime;          {ISO 8601}
    function EndOfTheWeek(const AValue: TDateTime): TDateTime;            {ISO 8601}
    function StartOfAWeek(const AYear, AWeekOfYear: Word;                 {ISO 8601}
      const ADayOfWeek: Word = 1): TDateTime;
    function EndOfAWeek(const AYear, AWeekOfYear: Word;                   {ISO 8601}
      const ADayOfWeek: Word = 7): TDateTime;function StartOfTheDay(const AValue: TDateTime): TDateTime;
    function EndOfTheDay(const AValue: TDateTime): TDateTime;
    function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
    function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; overload;
    function StartOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;
    function EndOfADay(const AYear, ADayOfYear: Word): TDateTime; overload;{ This of that functions }function MonthOfTheYear(const AValue: TDateTime): Word;
    function WeekOfTheYear(const AValue: TDateTime): Word; overload;      {ISO 8601}
    function WeekOfTheYear(const AValue: TDateTime;                       {ISO 8601}
      var AYear: Word): Word; overload;
    function DayOfTheYear(const AValue: TDateTime): Word;
    function HourOfTheYear(const AValue: TDateTime): Word;
    function MinuteOfTheYear(const AValue: TDateTime): LongWord;
    function SecondOfTheYear(const AValue: TDateTime): LongWord;
    function MilliSecondOfTheYear(const AValue: TDateTime): Int64;function WeekOfTheMonth(const AValue: TDateTime): Word; overload;    {ISO 8601x}
    function WeekOfTheMonth(const AValue: TDateTime; var AYear,          {ISO 8601x}
      AMonth: Word): Word; overload;
    function DayOfTheMonth(const AValue: TDateTime): Word;
    function HourOfTheMonth(const AValue: TDateTime): Word;
    function MinuteOfTheMonth(const AValue: TDateTime): Word;
    function SecondOfTheMonth(const AValue: TDateTime): LongWord;
    function MilliSecondOfTheMonth(const AValue: TDateTime): LongWord;function DayOfTheWeek(const AValue: TDateTime): Word;                 {ISO 8601}
    function HourOfTheWeek(const AValue: TDateTime): Word;                {ISO 8601}
    function MinuteOfTheWeek(const AValue: TDateTime): Word;              {ISO 8601}
    function SecondOfTheWeek(const AValue: TDateTime): LongWord;          {ISO 8601}
    function MilliSecondOfTheWeek(const AValue: TDateTime): LongWord;     {ISO 8601}
      

  4.   

    行了,谢谢gzmhero(hihihi),不过还有一个小问题能不能赠送啊,呵呵,不好意思季度应该如何调整?周计算我只要周一到周5应该如何调整?或者给一点注释行不?谢谢!
      

  5.   

    季度就按照
    MonthOfTheYear()来判断分割。1--3,4--6,7--9,10--12,4个区间来分。周计算,到周五,那显示的时候改下:            if MonthOf(dtTemp+iDays1)<>MonthOf(dtTemp) then
                begin
                    Memo2.Lines.Add('    本月第'+IntToStr(iCnt1)+'周:'+FormatDateTime('yyyy-mm-dd',dtTemp+iLastPos)+'----'+FormatDateTime('yyyy-mm-dd',(dtTemp+iDays-3)));
                    break;
                end else
                begin
                    Memo2.Lines.Add('    本月第'+IntToStr(iCnt1)+'周:'+FormatDateTime('yyyy-mm-dd',dtTemp+iLastPos)+'----'+FormatDateTime('yyyy-mm-dd',(dtTemp+iDays1-3)));
                    inc(iCnt1);
                    iLastPos:=iDays1;
                end;
      

  6.   

    谢谢gzmhero(hihihi),我编译的时候今天又出现了一个问题:在
     iDays:=DaysInMonth(dtTemp);
    [Error] CESmain.pas(2645): Incompatible types: 'Word' and 'TDateTime'
    咋回事呢?劳驾再帮忙看一下。晕死我了。
      

  7.   

    你的定义有问题看看dtTemp定义的是什么类型。是不是定义成了Word类型。
      

  8.   

    不是啊老大,你的代码完整COPY下来的,没有改啊,我再试一下。
      

  9.   

    你是把iDays定义成了TDateTime类型?
      

  10.   

    gzmhero(hihihi),呵呵,说了你都不相信,我怎么试都不行,后来解决了,真是晕死。
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, ImgList, Menus, ToolWin,StdCtrls, ExtCtrls,
      OleServer, OutlookXP, IdBaseComponent, IdMailBox, Grids, DBGrids, DB,
      DBTables, DBCtrls, ExcelXP, AxCtrls, OleCtrls, VCF1, Mask, dbcgrids,
      Buttons,DateUtils,TeEngine, Series, TeeProcs, Chart;看好DateUtils的位置,在这个位置上通不过,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, ImgList, Menus, ToolWin,StdCtrls, ExtCtrls,
      OleServer, OutlookXP, IdBaseComponent, IdMailBox, Grids, DBGrids, DB,
      DBTables, DBCtrls, ExcelXP, AxCtrls, OleCtrls, VCF1, Mask, dbcgrids,
      Buttons,TeEngine, Series, TeeProcs, Chart, DateUtils,;
    换了个位置,放到最后面就通过了,可能是BUG吧,呵呵,真服了!
    你的代码没有问题,是delphi的问题,谢谢你gzmhero(hihihi),!