delphi中日期格式的设定

本文介绍如何通过Forms.TApplication.UpdateFormatSettings解决XP和Win7系统中日期格式不一致的问题,提供代码示例,确保应用程序日期时间格式不受系统设置影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XP和Win7的日期格式在系统默认中并不一致,也许会对编程造成困扰,不是吗?

  设定系统日期的默认格式可以改变这一问题,可是这听起来并不完美。

  通过Forms.TApplication.UpdateFormatSettings设定可以很好的解决问题。这是Help里的描述:

Specifies whether format settings are updated automatically when the user alters the system configuration.

Use UpdateFormatSettings to control automatic updating of format settings. The default of true is set in the constructor.UpdateFormatSettings is checked when the application receives a WM_WININICHANGE message.

Using the default format settings is recommended. These settings are initialized to the Windows local settings. 

 通过UpdateFormatSettings我们可以手动设定程序所要使用的日期时间格式,代码如下

 

//锁定程序日期格式不受系统变化的影响
Application.UpdateFormatSettings := false;

//设定程序本身所使用的日期时间格式
LongDateFormat  := 'yyyy/MM/dd';
ShortDateFormat := 'yyyy/MM/dd';
LongTimeFormat  := 'hh:nn:ss';
ShortTimeFormat := 'hh:nn:ss';
DateSeparator := '/';
TimeSeparator := ':';

这样程序中使用的时间格式就不会再受时间变化影响。

 

以下的代码或许更全面:

procedure Tposmain.sys_format;

begin

 CurrencyString:='¥';   // 货币符号

 currencyformat:=0;   // 货币格式

 negcurrformat:=2;

 thousandseparator:=',';  // 货币千分位数分隔符号

 decimalseparator:='.';   // 小数点符号

 currencydecimals:=2;  // 货币小数位数

 dateseparator:='-';  // 日期分隔符

 shortdateformat:='yyyy-mm-dd'; // 短日期格式

 longdateformat:='yyyy-mm-dd'; // 长日期格式

 timeSeparator:=':'; // 时间 分隔符

 shortTimeFormat:='hh:mm:ss'; // 短时间格式

 longtimeformat:='hh:mm:ss'; //长时间格式

//  TimeAMString:='AM'; // 上午

//  TimePMString:='PM'; // 下午

 application.UpdateFormatSettings:=false;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值