查询日期字段日期格式用MM/DD/YY,如何使用中国人习惯的YY-MM-DD格式?(100分)

  • 主题发起人 主题发起人 zhcg
  • 开始时间 开始时间
Z

zhcg

Unregistered / Unconfirmed
GUEST, unregistred user!
我查询日期型字段时,把日期格式改为MM/DD/YY格式,这样才没出错。我看到别人的程序是
YY-MM-DD格式,控制面板也是这样,怎么不再使用MM/DD/YY格式也能正确查询到记录?
 
我这儿有一个控件,可以设定日期显示的格式,是从TCustomEdit继承而来,
可以实现你需要的功能.
源代码如下:
//用于输入Date数据的Edit控件, 添加了以下属性:
// Alignment: taLeftJustify, taRightJustify, taCenter. 指定对齐方式.
// DateValue: 用于读取/设定Edit控件的Date数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// Format: 用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.

unit DateEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;

type
TDateEdit = class(TCustomEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FDate: TDate;
FErrorMessage: String;
FErrorTitle: String;
FFormat: String;
//用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetAlignment: TAlignment; virtual;
function GetDate: TDate; virtual;
procedure SetAlignment(NewAlignment: TAlignment); virtual;
procedure SetDate(NewDate: TDate); virtual;
procedure SetFormat(NewFormat: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Alignment: TAlignment read GetAlignment write SetAlignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property DateValue: TDate read GetDate write SetDate;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property Format: String read FFormat write SetFormat;
property HideSelection;
property ImeMode;
property ImeName;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Standard', [TDateEdit]);
end;

procedure TDateEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;

procedure TDateEdit.DoExit;
var
TempDateTime: TDateTime;
TempColor: TColor;
TempString: String;
Index: Integer;
begin
inherited DoExit;
TempString := Text;
Index := 1;
while (Index <= Length(TempString)) and (TempString[Index] >= '0') and
(TempString[Index] <= '9') do Inc(Index);
if Index > Length(TempString) then
begin
case Length(TempString) of
6 : TempString := Copy(TempString, 1, 2) + '-' + Copy(TempString, 3, 2) + '-' + Copy(TempString, 5, 2);
8 : TempString := Copy(TempString, 1, 4) + '-' + Copy(TempString, 5, 2) + '-' + Copy(TempString, 7, 2);
end;
end
else begin
for Index := 1 to Length(TempString) do
if (TempString[Index] < '0') or (TempString[Index] > '9') then
TempString[Index] := '-';
end;
if not TryStrToDateTime(TempString, TempDateTime) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else DateValue := TempDateTime;
end;

function TDateEdit.GetAlignment: TAlignment;
begin
GetAlignment := FAlignment;
end;

function TDateEdit.GetDate: TDate;
begin
Result := FDate;
end;

procedure TDateEdit.SetAlignment(NewAlignment: TAlignment);
begin
if FAlignment <> NewAlignment then
begin
FAlignment := NewAlignment;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end;

procedure TDateEdit.SetDate(NewDate: TDate);
begin
FDate := NewDate;
Text := FormatDateTime(FFormat, FDate);
end;

procedure TDateEdit.SetFormat(NewFormat: String);
begin
if FFormat <> NewFormat then
begin
FFormat := NewFormat;
DateValue := FDate;
end;
end;

constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
DateValue := Now;
FErrorMessage := 'Invalid Date Type.';
FErrorTitle := 'Input Error:';
Format := 'yyyy''/''mm''/''dd';
end;

destructor TDateEdit.Destroy;
begin
inherited Destroy;
end;

end.





 
看你使用的是什么类型的数据库?
 
在工程单元中
DateSeparator :='-';
ShortDateFormat :='yyyy-mm-dd';
 
楼上的已经说的比较详细了,我就不说了。
 
各位看清楚题目了吗?
他说的是查询的时候,而不是显示的时候改变格式。
有两种方法:
一、查询的时候用日期转换函数转成数据库识别的格式,在SQL语句里做。
如ORACLE里的TO_DATE函数等
二、接受用户输入后转换成数据库识别的格式再传给SQL语句,在DELPHI中做。
用FormatDateTime函数即可
 
楼上说得对,说清楚你的数据库,也可以用参数来写进去。
 
多人接受答案了。
 
后退
顶部