Delphi SQL日期阅读问题 [英] Delphi SQL Date reading issue

查看:162
本文介绍了Delphi SQL日期阅读问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个集成了SQL连接的Delphi应用程序。

SQL以YYYY-MM-DD格式存储日期值,但是当我在Delphi应用程序中读取这些值时,以MM-DD-YYYY格式读取。

I am currently creating a Delphi Application with SQL connections integrated.
SQL stores its date values in the format YYYY-MM-DD, however, when I read these values within my Delphi application, they are read in the format MM-DD-YYYY.

我目前写的代码是从我的数据库读取日期的值,但是我在处理没有2个月整数或2天整数的值时遇到麻烦。例如,2013年2月17日的日期将被读为2/17/2013​​

I have currently written the code to read the values of the dates from my database, however I am having trouble processing values which do not have 2 month integers or 2 day integers. For example, the date 17th of February 2013 would be read as '2/17/2013'

我当前的代码将日期分为日,月和年。它适用于完整日期(MM / DD / YYYY),因为它使用Delphi中的复制命令读取文本中的相应值。当它读取格式为M / D / YYYY的日期时,它不会从字符串复制正确的值。

My current code separates the date into day, month and year. It works well for full dates (MM/DD/YYYY) as it reads the corresponding values within the text using the 'Copy' command within Delphi. When it reads a date which is in the format M/D/YYYY it does not copy the correct values from the string.

在运行我的分隔代码之前,有任何方法可以编辑字符串,因此它强制字符串格式为MM / DD / YYYY。我的想法是编写代码检查字符串的格式,然后添加任何零缺失:例如将2/17/2013变成02/17/2013

Is there any way to edit the string before I run my separation code, so that it forces the string into the format of MM/DD/YYYY. My idea is to write code to check the format of the string and then add any zeros which are missing: i.e change 2/17/2013 into 02/17/2013

我知道我可能做一些事情使事情变得更难为自己,但这是我的代码在这个时间点,所以任何帮助,通过添加到我当前的代码来解决我的问题将非常感谢。

I know I'm probably doing something to make things harder for myself, but this is my code at this point in time, so any help to fix my problem by adding to my current code would be much appreciated.

感谢,A。

推荐答案

不会将TDateTime值视为字符串,而是作为日期和时间。

As a general rule, don't treat TDateTime values as strings, but as dates and times.

不要使用AsString方法获取日期/时间字段的值,请使用AsDateTime方法并将其分配给TDateTime变量。

Don't get the value of a Date/Time field with the AsString method, use the AsDateTime method and assign it to a TDateTime variable.

如果您想知道日期部分,请使用提供的函数执行此操作。例如 DateUtils 单元中提供的功能。 SysUtils 单元还包含一些与日期/时间相关的功能。

If you want to know the date parts, use the provided functions to do so. For example the ones available in the DateUtils unit. The SysUtils unit also contains some Date/Time related functions.

uses
  DateUtils, SysUtils;

var
  MyDate: TDateTime;
  MyDay, MyMonth, MyYear: Word;
begin
  MyDate := MyQuery.Fields[3].AsDateTime;  //not AsString
  MyDay := DayOf(MyDate);
  MyMonth := MonthOf(MyDate);
  MyYear := YearOf(MyDate);
  ShowMessage(Format('Day: %d, Month: %d, Year: %d', [MyDay, MyMonth, MyYear]);

  //or also
  MyDate := EndOfTheMonth(MyDate);
  DecodeDate(MyDate, MyYear, MyMonth, MyDay);
  ShowMessage(Format('Day: %d, Month: %d, Year: %d', [MyDay, MyMonth, MyYear]);

这同样适用于将值存储到数据库,固定日期格式,使用参数,如下所示:

The same applies to storing values to the database, rather than use a fixed date format, use parameters, like this:

uses
  DateUtils, SysUtils;

var
  MyDate: TDateTime;
  MyDay, MyMonth, MyYear: Word;
begin
  MyDate := EncodeDate(2013, 2, 17);
  MyQuery.SQL.Text := 'insert into myTable (MyDate) values (:MyDate)';
  MyQuery.Params.ParamByName('MyDate').AsDateTime := MyDate;
  MyQuery.ExecSQL();

它适用于我所知道的所有数据库访问层。

It works with all the database access layers available that I know of.

这篇关于Delphi SQL日期阅读问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆