简体   繁体   English

如何使用delphi xe3更改系统shortdatetime格式

[英]How to change the system shortdatetime format using delphi xe3

I am going to try and make the question as simple as possible. 我将尝试使问题尽可能简单。

How do i either convert the system date to a format i would like, and still keep it a date and not a string. 我如何将系统日期转换为我想要的格式,并且仍然将其保留为日期而不是字符串。 Or how do i get the system's date format, to adjust my dates accordingly. 或如何获取系统的日期格式,以相应地调整我的日期。

When i call 当我打电话

FormatShortdateTime('d/M/yyyy',Date);

I get the correct date as string, but cannot convert it back to a Tdate and use it, then it clashes with the system date settings. 我以字符串形式获取了正确的日期,但是无法将其转换回Tdate并使用它,因此它与系统日期设置冲突。

If i can get the system shortdate format the problem would be solved. 如果我可以获取系统短日期格式,则该问题将得到解决。

Update 更新资料

The answer below refers to the original question that you asked. 下面的答案是指您提出的原始问题。 You've edited the question multiple times now to ask different questions. 您已多次编辑问题以提出不同的问题。 You really should not do that and it is a sign that you should spend more time understanding the problem before asking questions. 您确实不应该这样做,这表明您应该在提出问题之前花更多的时间来理解问题。

Even so, the final part of the answer, probably still contains the advice that you need. 即使如此,答案的最后部分可能仍包含您需要的建议。 Namely to stop using conversion functions that rely on the global FormatSettings variable, and always use conversion functions that are passed format settings as a parameter. 即停止使用依赖于全局FormatSettings变量的转换函数,而始终使用通过格式设置作为参数传递的转换函数。


The date values in your database are not stored in the format that you expect. 数据库中的日期值未以您期望的格式存储。 This program: 该程序:

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  fs: TFormatSettings;
begin
  fs := TFormatSettings.Create;
  fs.ShortDateFormat := 'd/M/yyyy';
  Writeln(DateToStr(StrToDate('18/2/2014', fs)));
end.

produces the following output on my machine: 在我的机器上产生以下输出:

18/02/2014

I guess on your machine it would produce a different output because your short date format is different from mine. 我猜您的机器上会产生不同的输出,因为您的短日期格式与我的不同。 But the point is that the call to StrToDate succeeds. 但关键是对StrToDate的调用成功。

Clearly the values stored in your database are not in the form you claim. 显然,存储在数据库中的值不是您声明的形式。 Because you claim that the above code would lead to a date conversion error in the call to StrToDate . 因为您声称上面的代码会在调用StrToDate导致日期转换错误。


Let's step back and look at the initial part of your question: 让我们退后一步,看看问题的最初部分:

I would like to know how to convert dates without having the system date influence the software. 我想知道如何在不影响系统日期的情况下转换日期。

The code in the answer above gives you an example of how to do that. 上面答案中的代码为您提供了执行此操作的示例。 Modern versions of Delphi overload the date and time conversion functions. 现代版本的Delphi重载了日期和时间转换功能。 There are overloads that accept TFormatSettings parameters, and overloads without such a parameter. 有接受TFormatSettings参数的重载,而没有此类参数的重载。 The TFormatSettings parameter is used to control the format used for the conversion. TFormatSettings参数用于控制用于转换的格式。 The overloads that do not receive such a parameter use the global FormatSettings instance declared in the SysUtils unit. 不接收这样的参数使用全局重载FormatSettings在声明实例SysUtils单位。

What you should do to avoid having the system settings influence your conversions is to only use conversion functions that accept a format settings parameter. 为避免系统设置影响转换,您应该做的是仅使用接受格式设置参数的转换功能。

On the other hand, if you want the system settings to influence your conversions, then feel free to call the overloads that use the global format settings variable. 另一方面,如果希望系统设置影响转换,则可以随时调用使用全局格式设置变量的重载。

My code above illustrates both. 我上面的代码说明了两者。 The conversion from string to date requires a fixed format, not varying with local system settings. 从字符串到日期的转换需要固定的格式,并且不会随本地系统设置而变化。 The conversion from date to string is designed to show a date using the prevailing local system settings. 从日期到字符串的转换旨在使用流行的本地系统设置来显示日期。

I would advice creating own TFormatSettings variable 我建议创建自己的TFormatSettings变量

older delphi versions 较旧的delphi版本

var
  fs: TFormatSettings;
begin
  fs := TFormatSettings.Create;
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, fs);
end.

This will create your own formatSettings (filled with system format) so you won't alter the default one. 这将创建您自己的formatSettings(填充系统格式),因此您不会更改默认设置。

Now what's important is that '/' in shortDateFormat will be replaced by dateSeparator so if you do this 现在重要的是shortDateFormat中的'/'将被dateSeparator替换,因此如果您这样做

fs.ShortDateFormat:='M/d/yyyy';
fs.dateSeparator:='?';

your input should be like this 您的输入应该像这样

strtodate('12?30?1899', fs);

or if other way around you will end up with string: '12?30?1899'; 否则,您将以以下字符串结尾:'12?30?1899';

I think that your problems originate from that you don't ask what dateSeparator is used. 我认为您的问题源于您不询问使用什么dateSeparator。 That would lead to problems if what program expects is '30-12-1899' but he gets '30/12/1899' from you. 如果程序期望的是“ 30-12-1899”,那会导致问题,但是他从您那里得到“ 30/12/1899”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM