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;
var T:Tdatetime; year,month,day:word; begin decodedate(datetimepicker1.datetime,year,month,day); T:=encodedate(year,month+1,1); datetimepicker1.date:=T-1;
var year,month : word; begin year := yearof(now); month := monthof(now); datetimepicker.date := endofmonth(year,month);
/////////////////////////////////////////////////////////////////////////// // 功能:获得某日期所在月份的最后一天 // // 入口参数: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;
像 dickeybird888 这样自己写函数当然好 但如果可以利用现有的函数解决问题,我觉得更好
var year,month : word; begin year := yearof(now); month := monthof(now); datetimepicker.date := endofmonth(year,month);
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);//最后一天
更正: var year,month : word; begin year := yearof(now); month := monthof(now); datetimepicker.date := endofamonth(year,month); 更正为endofamonth另: DateTimePicker1.Date := endofthemonth(now);首先在uses中调用DateUtils应该完整了,楼注结帖吧
t:TDateTime;
DecodeDate(t,y,m,d);
t := EncodeDate(y,m,1);
uses DateUtils;//不要忘记了它,不然你的IncDay函数可是用不了的。。datetimepicker1.datetime := IncDay(StrToDateTime(FormatDateTime('yyyy-MM-01',IncMonth(Date))),-1);
DecodeDate(t,y,m,d);
t := EncodeDate(y,m+1,1); //下个月的第一天
t:=t-1;
//b本月最后一天
Year, Month, Day: word;
DecodeDate(IncMonth(Date), Year, Month, Day);
datetimepicker1.datetime := IncDay(EncodeDate(Year, Month , 1),-1);
//返回某年某月的天数
//返回某年某月最后一天的日期和时间
年+月+DayCountDrate(小虫) 的方法有没有第12月份的错误?大家测试一下,我没有试。
delphi6有,也有更简单的函数EndOfTheMonth
//Return the count of days in a month
//Copy Right 549@11:48 2003-8-7
begin
Result := DayOf( EndOfTheMonth( theDay ) );
end;
T:Tdatetime;
year,month,day:word;
begin
decodedate(datetimepicker1.datetime,year,month,day);
T:=encodedate(year,month+1,1);
datetimepicker1.date:=T-1;
year,month : word;
begin
year := yearof(now);
month := monthof(now);
datetimepicker.date := endofmonth(year,month);
问题解决了,窗体有两个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;
但我觉得好像还有更简单的方法
// 功能:获得某日期所在月份的最后一天 //
// 入口参数: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;
但如果可以利用现有的函数解决问题,我觉得更好
year,month : word;
begin
year := yearof(now);
month := monthof(now);
datetimepicker.date := endofmonth(year,month);
T:Tdatetime;
year,month,day:word;
begin
decodedate(datetimepicker1.datetime,year,month,day);
datetimepicker1.date:=encodedate(year,month,1); //第一天
datetimepicker2.date:=endofmonth(year,month);//最后一天
var
year,month : word;
begin
year := yearof(now);
month := monthof(now);
datetimepicker.date := endofamonth(year,month);
更正为endofamonth另:
DateTimePicker1.Date := endofthemonth(now);首先在uses中调用DateUtils应该完整了,楼注结帖吧
怎么很多人还在使用DateUtils的函数回答问题?