关于DateTimePicker控件的使用(100分)

  • 主题发起人 主题发起人 shijie
  • 开始时间 开始时间
S

shijie

Unregistered / Unconfirmed
GUEST, unregistred user!
添加两个DateTimePicker控件,怎样写确定两个选定时间之间执行某个功能的代码。
 
select * from tabel where 日期>=#'+datetimetostr(D1)+'# and 日期<=#'+datetimetostr(D2)+'#
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a,b:integer;
begin
a:=Int(DateTimePicker1.Date);
b:=Int(DateTimePicker2.Date);
edit1:=inttostr(a);
edit2:=inttostr(b);
edit3:=inttostr(b-a);
end;

end.


编译后显示:
[Error] Unit1.pas(36): Incompatible types: 'Integer' and 'Extended'
[Error] Unit1.pas(37): Incompatible types: 'Integer' and 'Extended'
[Error] Unit1.pas(38): Incompatible types: 'TEdit' and 'String'
[Error] Unit1.pas(39): Incompatible types: 'TEdit' and 'String'
[Error] Unit1.pas(40): Incompatible types: 'TEdit' and 'String'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
DateTimePicker1.Date为日期型?类型不一致,这样转换类型不行?
 
类型不匹配
Uses DateUtils;
使用这个单元的以下函数来比较时间
YearsBetween()
MonthsBetween()
DaysBetween()
HoursBetween()
MinutesBetween()
SecondsBetween()
 
Uses DateUtils以后还是不行,DaysBetween()有几个参数?怎么用?
 
[:(],日期有你这种减法吗?还有Edit后面居然不带Text就负值。。。。。。。
 
var
a,b:Extended
a:=DateTimePicker1.Date;
b:=DateTimePicker2.Date
edit1.Text:=inttostr(a);
edit2.Text:=inttostr(b);
edit3.Text:=inttostr(b-a);
 
看来这位刚开始学Delphi。加油啊!
 
Uses DateUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
a,b: TDateTime;
begin
a := StrToDateTime('2008-4-19 20:12:45');
b := StrToDateTime('2008-4-10 2:12:45');
Edit1.Text := IntToStr(DateUtils.DaysBetween(a,b));
end;
 
tandxu的代码编译不能通过,请看:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DateUtils, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a,b:Extended;
begin
a:=DateTimePicker1.Date;
b:=DateTimePicker1.Date;
edit1.Text:=inttostr(a);
edit2.Text:=inttostr(b);
edit3.Text:=inttostr(b-a);
end;

end.

[Error] Unit1.pas(37): There is no overloaded version of 'IntToStr' that can be called with these arguments
[Error] Unit1.pas(38): There is no overloaded version of 'IntToStr' that can be called with these arguments
[Error] Unit1.pas(39): Undeclared identifier: 'edit3'
[Error] Unit1.pas(39): There is no overloaded version of 'IntToStr' that can be called with these arguments
[Fatal Error] Project22.dpr(5): Could not compile used unit 'Unit1.pas'
 
liyinwei的代码是这样?编译可通过,结果不对。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DateUtils, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a,b:TDate;
begin
a:=DateTimePicker1.Date;
b:=DateTimePicker2.Date;
edit1.Text:=datetostr(a);
edit2.Text:=datetostr(b);
edit3.Text:=datetostr(DateUtils.DaysBetween(a,b));
end;

end.
 
哎!我贴出来代码楼主有认真的看了吗?
DaysBetween 的返回值是 Integer 类型,不是 TDate ,用 DateToStr 进行转换当然不正确了.
edit3.Text:=Inttostr(DateUtils.DaysBetween(a,b));
 
liyinwei:
您的代码我看了。
a := StrToDateTime('2008-4-19 20:12:45');
b := StrToDateTime('2008-4-10 2:12:45');
两句中没有用DateTimePicker,本人就做了一个改动,由于本人对DaysBetween缺乏认识,就又做了改动,DaysBetween返回的是天数,故应为Integer 类型,又出了错误。
 
unit Unit1;
interface
uses
Windows, Messages, DateUtils, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin

Edit1.Text := IntToStr(DateUtils.DaysBetween(DateTimePicker1.date,DateTimePicker2.date));
end;

end.
这样可以算天数
 
时间的先后次序能否判断?有这样的API函数吗?
 
有简单方法,用format('yyyy-mm-dd',DateTimePicker1.date),format返回值的类型是string 。
select * from tabel where 日期 between format('yyyy-mm-dd',DateTimePicker1.date)and format('yyyy-mm-dd',DateTimePicker2.date)
 
不用SQL语句能否判断时间的先后次序?
 
晕死,不忍看下去了,乱搞,DFW啥时落魄到如此地步了[:(]
 
[:D]select * from tabel where 日期 between :a and :b
open前用:query1.parameters[0].value := datetimepicker1.date;
query1.parameters[1].value := datetimepicker2.date;
 
爱钻牛角尖,
 
后退
顶部