将日期转换为年、月、日日期时间格式

发布于 2025-01-07 07:31:10 字数 451 浏览 4 评论 0原文

我研究 TDateTime 函数和过程,但没有找到允许我将天数转换为等效年、月、日的东西,例如,如果我有天数= 0,我应该得到结果:

Year:   0
Month:  0
Day:    0

或者我有天数= 1 ,应该有:

Year:   0
Month:  0
Day:    1   // Just 1 day 

或者如果我有天= 32:

Year:   0
Month:  1  // January is 31 days
Day:    1  // Day is 1 
           // Total are 32 days 

等等,也考虑到二月有 29 天而不仅仅是 28 天。 在delphi xe2中,谁能告诉我它是什么函数,或者是否需要手动编写?非常感谢。

i studying TDateTime functions and procedure, but not found something that allow me to convert a number of days in equivalent Year, Month, Day, for example, if i have days = 0 i should to have as result:

Year:   0
Month:  0
Day:    0

or i have days = 1, should have:

Year:   0
Month:  0
Day:    1   // Just 1 day 

or if i have days = 32:

Year:   0
Month:  1  // January is 31 days
Day:    1  // Day is 1 
           // Total are 32 days 

etc, considering too when february month is of 29 days and not only 28.
In delphi xe2, who can tell me what function do it, or if need write it manually? Thanks very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2025-01-14 07:31:10

使用 DateUtils 中的方法:

procedure daysToDate(days: Integer; out day, month, year: Integer);
var
  january1st: TDateTime;
  targetDate: TDateTime;
begin
  january1st := StartOfTheYear(Now());
  targetDate := IncDay(january1st, days);

  day := DayOfTheMonth(targetDate) - 1;
  month := MonthOfTheYear(targetDate) - 1;
  year := YearOf(targetDate) - YearOf(january1st);
end;

此代码假设您正在计算当年的天数。如果您想从不同的年份开始,请根据需要修改 january1st := StartOfTheYear(Now()); 行。

Using the methods in DateUtils:

procedure daysToDate(days: Integer; out day, month, year: Integer);
var
  january1st: TDateTime;
  targetDate: TDateTime;
begin
  january1st := StartOfTheYear(Now());
  targetDate := IncDay(january1st, days);

  day := DayOfTheMonth(targetDate) - 1;
  month := MonthOfTheYear(targetDate) - 1;
  year := YearOf(targetDate) - YearOf(january1st);
end;

This code assumes that you are counting the days in the current year. If you want to start in a different year then modify the line january1st := StartOfTheYear(Now()); as needed.

梦屿孤独相伴 2025-01-14 07:31:10

我不太明白你的问题。

如果您想翻译几天(在某些时间跨度内):

没有什么完全像这样,原因很简单,它没有明确定义,原因如下:不是每个月有相同的天数。因此,输入不能仅为天数。例如,如果输入“35天”,结果应该是什么?应该是“1个月零4天”还是“1个月零5天”、“1个月零6天”还是“1个月零7天”?相反,您需要指定初始日期和最终日期。

现在,在 DateUtils 中,您有 DaysBetweenMonthsBetweenYearsBetween,但这些是 em> 只是近似值。

如果您可以接受近似值,那么这很容易:年数等于天数的整数部分除以 365.24,月数等于小数部分乘以 12,依此类推。

如果你想获取某年的第N天:

IncDay(StartOfTheYear(SomeYear), N)

I don't really understand your question.

If you want to translate a number of days (in some timespan):

There is nothing exactly like this, for the simple reason that it would not be well-defined, for the reason you give: not every month has the same number of days. Hence, the input cannot be only a number of days. For example, what should the result be if you input "35 days"? Should it be "1 month, 4 days" or "1 month, 5 days", "1 month, 6 days", or "1 month, 7 days"? Instead, you need to specify the initial and finial dates.

Now, in DateUtils, you have DaysBetween, MonthsBetween, and YearsBetween, but these are only approximate.

If you can live with approximate values, it is easy, though: The number of years equals the integer part of the number of days divided by 365.24, the number of months equals the fractional part times 12, and so on.

If you want to obtain the Nth day of some year:

IncDay(StartOfTheYear(SomeYear), N)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文