如何知道某一天处于第几个星期?(100分)

  • 主题发起人 主题发起人 strongm0002
  • 开始时间 开始时间
S

strongm0002

Unregistered / Unconfirmed
GUEST, unregistred user!
例如:现在是7月6日了, 那么今年已经过了多少个星期了? 或者说今天是处于今年的第几个星期?
 
现看今年的第一天是星期几。
再算出总天数。
就可以搞定。
 
{
输入日期的在该年的周数
Modified By wgl From JCL
返回整数,表明是一年中的第几周,周数以礼拜一开始
第一周是必须包括礼拜四,等于包括了1月4日的那一周是第一周
}
function WeekOfYear( dDate: TDateTime ): Integer;
const
Fiddle: array [1..7] of Byte = (6, 7, 8, 9, 10, 4, 5);
var
Present, StartOfYear: TDateTime;
FirstDayOfYear, WeekNumber, NumberOfDays: Integer;
Year, Month, Day: Word;
begin
Present := Trunc(dDate)
// truncate to remove hours, mins and secs
DecodeDate(Present, Year, Month, Day)
// decode to find year
StartOfYear := EncodeDate(Year, 1, 1)
// encode 1st Jan of the year
// find what day of week 1st Jan is, then add days according to rule
FirstDayOfYear := Fiddle[DayOfWeek(StartOfYear)];
// calc number of days since beginning of year + additional according to rule
NumberOfDays := Trunc(Present - StartOfYear) + FirstDayOfYear;
// calc number of weeks
WeekNumber := NumberOfDays div 7;
if WeekNumber = 0 then
// see if previous year end was week 52 or 53
Result := WeekOfYear(EncodeDate(Year - 1, 12, 31))//, YearOfWeekNumber)
else
begin
// YearOfWeekNumber := Year;
Result := WeekNumber;
if WeekNumber = 53 then
begin
// if 31st December less than Thursday then must be week 01 of next year
if DayOfWeek(EncodeDate(Year, 12, 31)) < 5 then
begin
// YearOfWeekNumber := Year + 1;
Result := 1;
end;
end;
end;
end;
 
WeeksInYear function
Returns the number of weeks in the year of a specified TDateTime value.

Unit
DateUtils
Category
date/time routines

function WeeksInYear(const AValue: TDateTime): Word;

Description

Call WeeksInYear to obtain the number of weeks in the year of the TDateTime
value specified by AValue.

Note: WeeksInYear defines the first week of the year according to the ISO
8601 standard. That is, the first week of the year is the one that includes the
first Thursday of the year (the first week that has 4 or more days in the year).
This means that WeeksInYear always returns either 52 or 53.
 
浦欣兄 WeeksInYear 返回的是一年有几个礼拜
不是这个日期是哪一天 .我那一段才是正确的.也是ISO组织规定的取周数的方法
 
to LukeWang:
不好意思,眼花了!
function WeekOfTheYear(const AValue: TDateTime): Word
overload;
function WeekOfTheYear(const AValue: TDateTime
var AYear): Word
overload;
这个应该是吧!
 
[载自中文开发在线http://www.codelphi.com]
星期速算
jangill


历史上的某一天是星期几,可利用高斯函数,我们可根据设闰的规律,推算出公元X年第Y天是星期几,这里变量X是公元的年数,变量Y是从这一年元旦算起,至这一天为止的天数(含这一天),历法家为我们找到了这样一个公式:

S=(X-1)+[(X-1)/4]-[(X-1)/100]+[(X-1)/400]+Y
(注:这里的是高斯函数,S是它的变量,可以是整型,也可以是实型,意为取不大于S的最大整数)
按上式算出S后,除以7,余几即为星期几,若除尽,则为星期日.
例:1994年1月14日是星期几,
S=(1994-1)+[(1994-1)/4]-[(1994-1)/100]+[(1994-1)/400]+14
=1993+498-19+4+14
=2490
S除以7商355余5,所以 1994年1月14日是星期五.

我自制了一个函数 GETWEEK(),如下:

FUNCTION GETWEEK(CONST DA:TDATE):INTEGER

VAR DASTR:STRING

YEAR,MONTH,DAY,DAYS,SUM,DAY1,DAY2,I:INTEGER

BEGIN
DASTR:=DATETOSTR(DA)


YEAR:=STRTOINT(DASTR[1]+DASTR[2]+DASTR[3]+DASTR[4])

MONTH:=STRTOINT(DASTR[6]+DASTR[7])

DAY:=STRTOINT(DASTR[9]+DASTR[10])


IF ((YEAR MOD 400)=0 ) OR (((YEAR MOD 4)=0) AND ((YEAR MOD 100)<> 0))
THEN DAY2:=29
ELSE DAY2:=28


//GET_DAYS

DAYS:=0

FOR I:=1 TO MONTH-1 DO
BEGIN
CASE I OF
1,3,5,7,8,10,12: DAY1:=31

4,6,9,11: DAY1:=30

2: DAY1:=DAY2

END


DAYS:=DAYS+DAY1

END


DAYS:=DAYS+DAY


SUM:=(YEAR-1) + ((YEAR-1) DIV 4) - ((YEAR-1) DIV 100) +((YEAR-1) DIV 400) +DAYS


GETWEEK:=SUM MOD 7

END


本程序在DELPHI5.0,WINDOWS98SE 环境下运行通过。


投稿人:jangill 投稿日期:2001-8-9 9:22:00
 
不用这么复杂吧, 我记得有个函数,dayofweek (具体名字记不清了,没有帮助无法查看)
直接调用就可以了, 以日期作参数, 返回0(或1、2、3、4、5、6)对应星期几了
 
To ALL: 对不起,看错题了
 
WeekOfTheYear
Call WeekOfTheYear to obtain the week of the year represented
by a specified TDateTime value.
WeekOfTheYear returns a value between 1 and 53.
 
后退
顶部