如题

解决方案 »

  1.   

    var y,m,d:word;
    t:TDateTime;
     DecodeDate(t,y,m,d);
     t := EncodeDate(y,m,1);
      

  2.   

    呵呵,用一个取巧的办法,取下个月的第一天,然后再减一天就是了:
    uses DateUtils;//不要忘记了它,不然你的IncDay函数可是用不了的。。datetimepicker1.datetime := IncDay(StrToDateTime(FormatDateTime('yyyy-MM-01',IncMonth(Date))),-1);
      

  3.   

    t:TDateTime;
     DecodeDate(t,y,m,d);
     
     t := EncodeDate(y,m+1,1); //下个月的第一天
    t:=t-1;
    //b本月最后一天
      

  4.   

    var
      Year, Month, Day: word;
    DecodeDate(IncMonth(Date), Year, Month, Day);
    datetimepicker1.datetime := IncDay(EncodeDate(Year, Month , 1),-1);
      

  5.   

    function DaysInAMonth(const AYear,AMonth: Word): Word;
    //返回某年某月的天数
      

  6.   

    function EndOfAMonth(const AYear,AMount: Word): TDateTime;
    //返回某年某月最后一天的日期和时间
      

  7.   

    我用的是DayCount
    年+月+DayCountDrate(小虫) 的方法有没有第12月份的错误?大家测试一下,我没有试。
      

  8.   

    我用的delphi5没有这个DateUtils
    delphi6有,也有更简单的函数EndOfTheMonth
      

  9.   

    function DayCount( theDay : TDateTime) : Integer;
    //Return the count of days in a month
    //Copy Right 549@11:48 2003-8-7
    begin
      Result := DayOf( EndOfTheMonth( theDay ) );
    end;
      

  10.   

    var
    T:Tdatetime;
    year,month,day:word;
    begin
    decodedate(datetimepicker1.datetime,year,month,day);
    T:=encodedate(year,month+1,1);
    datetimepicker1.date:=T-1;
      

  11.   

    var
      year,month : word;
    begin
      year := yearof(now);
      month := monthof(now);
      datetimepicker.date := endofmonth(year,month);
      

  12.   

    感谢大家回复
    问题解决了,窗体有两个DateTimePicker,一个要设置成月首,一个设置成月末
    ls_DateTime := FormatDateTime('yyyymmdd',Date);
    DTP_begin.DateTime := StrToDate(Copy(ls_DateTime,0,4)+'-'
                                   +Copy(ls_DateTime,5,2)+'-'+'01');DTP_end.DateTime := IncMonth(DTP_begin.DateTime,1)-1;
    但我觉得好像还有更简单的方法
      

  13.   

    ///////////////////////////////////////////////////////////////////////////
    // 功能:获得某日期所在月份的最后一天                                    //
    // 入口参数:TDateTime 某日期                                            //
    // 返回值:  某日期所在月份的最后一天                                    //
    ///////////////////////////////////////////////////////////////////////////
    function MonthEnd(Date:TDateTime):TDateTime;
    var
     Year, Month, Day{, Hour, Min, Sec, MSec}: Word;
     T:String;
    begin
       Result:=0;
       DecodeDate(Date, Year, Month, Day);
       T:=IntToStr(Year)+'-'+IntToStr(Month)+'-';
       case Month of
        1,3,5,7,8,10,12:Result:=StrToDate(T+'31');
        4,6,9,11       :Result:=StrToDate(T+'30');
        2              :if (Year mod 4 =0) and ( Year mod 100 <> 0 )
                              or (Year mod 400 =0 )
                        then
                            Result:=StrToDate(T+'29')
                        else
                            Result:=StrToDate(T+'28');
       end;
    end;
      

  14.   

    像 dickeybird888 这样自己写函数当然好
    但如果可以利用现有的函数解决问题,我觉得更好
      

  15.   

    var
      year,month : word;
    begin
      year := yearof(now);
      month := monthof(now);
      datetimepicker.date := endofmonth(year,month);
      

  16.   

    var
    T:Tdatetime;
    year,month,day:word;
    begin
    decodedate(datetimepicker1.datetime,year,month,day);
    datetimepicker1.date:=encodedate(year,month,1); //第一天
    datetimepicker2.date:=endofmonth(year,month);//最后一天
      

  17.   

    更正:
    var
      year,month : word;
    begin
      year := yearof(now);
      month := monthof(now);
      datetimepicker.date := endofamonth(year,month);
    更正为endofamonth另:
    DateTimePicker1.Date := endofthemonth(now);首先在uses中调用DateUtils应该完整了,楼注结帖吧
      

  18.   

    楼主不是说delphi5没有DateUtils单元么?
    怎么很多人还在使用DateUtils的函数回答问题?