DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: melqui
今日帖子: 21
在线用户: 12
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/3/27 0:42:37
标题:
如何动态改变TDateEdit.date的值? 浏览:1146
加入我的收藏
楼主: 1、DateEdit1.date:=Now;  闪退
2、DateEdit1.date:=Strtodate('2022-02-25');   提示:2022-02-25 is not a valid date


如何给DateEdit1.date赋值呢?让DateEdit.date 指定为某年某月某日,非TEXT
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 pcplayer (pcplayer) ★☆☆☆☆ -
普通会员
2022/3/27 1:03:29
1楼: 如果能用 TDateTime 类型,就别用字符串。

如果非要把字符串变成 TDateTime ,那你得会变。

记得前几天有个贴就讨论了这个问题。你去查一查。

在 Delphi 里面,不能直接用 TDateTime 类型而只能用字符串的时候,非常少。
----------------------------------------------
-
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/3/27 1:42:46
2楼: Properties 里,Date:2022-03-05  Format:YYYY/MM/DD
一、
VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy-mm-dd';
  aF.DateSeparator:='-';
  DateEdit1.Date:=strtodate('2022-03-03',af);

结果:闪退

二、
Properties 里,Date:2022-03-05  Format:YYYY/MM/DD

VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy/mm/dd';
  aF.DateSeparator:='/';
  DateEdit1.Date:=strtodate('2022-03-03',aF);

结果:‘2022-03-03’ is not a valid date

三、
Properties 里,Date:2022-03-05  Format:YYYY/MM/DD
VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy/mm/dd';
  aF.DateSeparator:='/';
  DateEdit1.Date:=strtodate('2022/03/03',aF);

结果:闪退

四、Properties 里的 Format:(为空)
VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy/mm/dd';
  aF.DateSeparator:='/';
  DateEdit1.Date:=strtodate('2022/03/03',aF);

结果:闪退

五、Properties 里的 Format:(为空)
VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy-mm-dd';
  aF.DateSeparator:='-';
  DateEdit1.Date:=strtodate('2022-03-03',aF);

结果:闪退

六、Properties 里的 Format:yyyy-mm-dd
VAR aF:TFormatSettings;
begin
  aF:=TFormatSettings.Create('zh-CN');
  aF.ShortDateFormat:='yyyy-mm-dd';
  aF.DateSeparator:='-';
  DateEdit1.Date:=strtodate('2022-03-03',aF);

结果:闪退
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/3/27 2:17:01
3楼: 总结:
在DateEdit的properties的format是yyyy/mm/dd,
VAR af2:TFormatSettings;
begin
  af2.ShortDateFormat:='yyyy/mm/dd';
  aF2.DateSeparator:='/';
  DateEdit1.Date:=strtodate('2022/03/03',aF2);
是可以的
闪退的原因:
DateEDIT1,DATEEDIT2  是通过combo Box里的数值进行加一年的计算,ComboBox没有ComboBox1.ItemIndex:=0;
也就是incyear的参数其中一个为空值,导致闪退
procedure TCloundServicesCliAdd_Fm.DateEdit1Change(Sender: TObject);
begin
  DateEdit2.Date:=incyear(DateEdit1.Date,strtoint(ComboBox1.Selected.Text));
end;
给ComboBox1.ItemIndex:=0;后,incyear的第二个参数不为空,问题排除
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/3/27 3:09:07
4楼: var
  Form1: TForm1;

implementation

uses
  System.DateUtils;

{$R *.dfm}

var
  MyFS: TFormatSettings;

function MyNewDateTimeResult(AOldDateTimeInText: string; AYears:Byte = 1 {0..255}): TDateTime;
begin
  // using "dd/mm/yyyy" = pt-BR
  result := IncYear(StrToDateTimeDef(AOldDateTimeInText, 0, MyFS), AYears); // 0 = 30/12/1899 = none error!!!
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  //  Edit2.Text := DateTimeToStr(MyNewDateTimeResult(Edit1.Text), MyFS); --> 1 year by default
  Edit2.Text := DateTimeToStr(MyNewDateTimeResult(Edit1.Text, 1), MyFS);
  //

 ONCHANGE: using a DateComponent.DATE directly := MyNewDateTimeResult(Edit1.Text, 1);  // <---
end;

initialization

MyFS := TFormatSettings.Create;

// MyFS.DateSeparator := '?';
// MyFS.TimeSeparator := '?';
// etc...
// MyFS .... properties definition....
finalization

end.
此帖子包含附件:
PNG 图像
大小:6.5K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 sail2000 (小帆工作室) ★☆☆☆☆ -
盒子活跃会员
2022/3/27 19:54:54
5楼: 告诉过你了,别偷懒,要么用TFormatSettings格式化自己的应用程序的日期,要么自己用 Encodedate/decoddate 自己拼接.你不可能让用户的日期设置格式都和你自己电脑上的一样,你代码的错误就是源于这个.
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行125毫秒 RSS