基于系统周开始的Delphi周号功能 [英] Delphi week number function based on system start of week

查看:70
本文介绍了基于系统周开始的Delphi周号功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateUtils.WeekOfTheYear函数很棒,但是它使用一周的ISO 8601标准定义.也就是说,一周被认为从星期一开始,而在星期日结束.我需要一个类似的功能,该功能根据一周开始时的系统设置确定星期几.或至少像MySQL的Week函数一样,在星期的星期日或星期一开始.有人有这样的功能吗?

The DateUtils.WeekOfTheYear function is great but it uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday. I need a similar function that determines the week number based on the system setting for the start of the week. Or at least for either a sunday or monday start of week like MySQL's week function. Anyone have such a function?

推荐答案

如果您只对从星期日开始而不是星期一开始的一周感兴趣,则可以在输入日期之前先从您的 DateTime 值中减去1天到 DateUtils.WeekOfTheYear 函数.

If you are only interested in week starting on Sunday instead on Monday you can simply substract 1 day from your DateTime value before feeding it to DateUtils.WeekOfTheYear function.

对David Heffernan评论的回复:

Response to David Heffernan comment:

想象一下,如果从1月1日减去1,会发生什么情况

Imagine what happens when you subtract 1 from January 1st

这取决于1月1日是哪一天

It depends on which day is on January 1st

摘自Embarcadero文档: http://docwiki.embarcadero.com/库/XE8/en/System.DateUtils.WeekOfTheYear

From Embarcadero documentation: http://docwiki.embarcadero.com/Libraries/XE8/en/System.DateUtils.WeekOfTheYear

AYear返回指定星期发生的年份.注意这可能与AValue的年份不同.这是因为一年的第一周定义为四个或更多的第一周那年的日子.这意味着,如果年份是星期五,星期六或星期日,然后是前三个,两个,或日历年的某一天,WeekOfTheYear返回最后一周上一年的.同样,如果是一年中的最后一个日历日是星期一,星期二或星期三,然后是最后一个,两个或日历年的三天,WeekOfTheYear返回1(第一个下一个日历年的星期).

AYear returns the year in which the specified week occurs. Note that this may not be the same as the year of AValue. This is because the first week of a year is defined as the first week with four or more days in that year. This means that, if the first calendar day of the year is a Friday, Saturday, or Sunday, then for the first three, two, or one days of the calendar year, WeekOfTheYear returns the last week of the previous year. Similarly, if the last calendar day of the year is a Monday, Tuesday, or Wednesday, then for the last one, two, or three days of the calendar year, WeekOfTheYear returns 1 (the first week of the next calendar year).

因此,如果一周从星期日而不是星期一开始,则意味着一周的开始和结束日期仅向后移动了一天.

So if the week starts with Sunday instead of Monday then it means that week start and end days are simply shifted by one day backward.

因此,在这种情况下,最好使用带有附加变量参数的覆盖版本,该变量参数将存储本周所属的年份.

So for such occasions it is best to use over-ridden version with additional variable parameter to which the year that this week belongs to is stored.

这篇关于基于系统周开始的Delphi周号功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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