#11




RE: DateToWeek function?
Andy,
thanks for helping, but your function is incorrect too.. sorry. Take a look: if I use the original function you pasted, these are the results: 01011991 = 1 01011992 = 1 01011993 = 53 01011994 = 52 01011995 = 52 01011996 = 1 01011997 = 1 01011998 = 1 // ERROR: SHOULD BE 53 01011999 = 53 // ERROR: SHOULD BE 52 01012000 = 52 01012001 = 1 01012002 = 1 01012003 = 1 01012004 = 1 // ERROR: SHOULD BE 53 01012005 = 53 // ERROR: SHOULD BE 52 01012006 = 52 01012007 = 1 01012008 = 1 01012009 = 1 // ERROR: SHOULD BE 53 01012010 = 53 // ERROR: SHOULD BE 52 01012011 = 52 01012012 = 52 01012013 = 1 01012014 = 1 01012015 = 1 // ERROR: SHOULD BE 53 01012016 = 53 // ERROR: SHOULD BE 52 01012017 = 52 01012018 = 1 01012019 = 1 01012020 = 1 01012021 = 53 01012022 = 52 01012023 = 52 01012024 = 1 01012025 = 1 01012026 = 1 // ERROR: SHOULD BE 53 01012027 = 53 // ERROR: SHOULD BE 52 01012028 = 52 01012029 = 1 01012030 = 1 If I alter the shiftfase from 12 to 11, you'll see the errors on different spots. 01011991 = 1 01011992 = 1 01011993 = 53 01011994 = 52 01011995 = 1 // ERROR: SHOULD BE 52 01011996 = 1 01011997 = 1 01011998 = 53 01011999 = 52 01012000 = 52 01012001 = 1 01012002 = 1 01012003 = 1 01012004 = 53 01012005 = 52 01012006 = 1 // ERROR: SHOULD BE 52 01012007 = 1 01012008 = 1 01012009 = 53 01012010 = 52 01012011 = 52 01012012 = 1 // ERROR: SHOULD BE 52 01012013 = 1 01012014 = 1 01012015 = 53 01012016 = 52 01012017 = 1 // ERROR: SHOULD BE 52 01012018 = 1 01012019 = 1 01012020 = 1 01012021 = 53 01012022 = 52 01012023 = 1 // ERROR: SHOULD BE 52 01012024 = 1 01012025 = 1 01012026 = 53 01012027 = 52 01012028 = 52 01012029 = 1 01012030 = 1 Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= don't forget to accept this reply if it helped you! ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== 
#12




RE: DateToWeek function?
Andy,
i found the problem. It does not have to do with a wednesday/thursday problem, but the problem in your function is, that it's not a wednesday, but a friday that might be the first of the week. the problems come up when the friday is january 1st. so, it's not correctly writen here: http://www.merlyn.demon.co.uk/weekinfo.htm This won't work (1/2): var y, m, d: word; h: integer; FirstofJanuary, FirstDay, FirstWeekStart: TDateTime; begin Decodedate(date, y, m, d); FirstofJanuary := Encodedate(y, 1, 1); h := dayOfWeek(FirstofJanuary); FirstDay := FirstofJanuary + (( FirstWeekStart := FirstDay  3; if trunc(date) < FirstWeekStart then result := WeekNo(FirstofJanuary  1) (* 1231 of previous year *) else result := (round(trunc(date)  FirstWeekStart) div 7) + 1; end; Neither will this work (2/2): var y, m, d: word; h: integer; FirstofJanuary, FirstDay, FirstWeekStart: TDateTime; begin Decodedate(date, y, m, d); FirstofJanuary := Encodedate(y, 1, 1); h := dayOfWeek(FirstofJanuary); FirstDay := FirstofJanuary + (( FirstWeekStart := FirstDay  3; if trunc(date) < FirstWeekStart then result := WeekNo(FirstofJanuary  1) (* 1231 of previous year *) else result := (round(trunc(date)  FirstWeekStart) div 7) + 1; end; But this does the trick (in my case): var y, m, d: word; h: integer; FirstofJanuary, FirstDay, FirstWeekStart: TDateTime; begin Decodedate(date, y, m, d); FirstofJanuary := Encodedate(y, 1, 1); h := dayOfWeek(FirstofJanuary); FirstDay := FirstofJanuary + (( FirstWeekStart := FirstDay  3; if trunc(date) < FirstWeekStart then result := WeekNo(FirstofJanuary  1) (* 1231 of previous year *) else result := (round(trunc(date)  FirstWeekStart) div 7) + 1; end; Thanx all, for all your quick responses. I'll accept Andy's reply for helping me on track. Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= don't forget to accept this reply if it helped you! ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== 
#13




RE: DateToWeek function?
No, it is correct. For example in 1998 January 1st is a Thursday, that means that that week (if starting from Monday) has four days in 1998, and three days in 1997  thus it is week 1 (according to the ISO standard). And that of course also shifts the first week of 1999 by 1.
By the distribution of week 1 vs. week 52/53 you seem to use a more unusual definition of week 1, as if the majority of the week makes it week 1 it means that 4 out of 7 years January 1st is in week 1  but you have 4 out of 7 with week 52/53. I can produce your pattern by changing my code to this one: FirstWednesday:=FirstofJanuary+((11h) mod 7); FirstWeekStart:=FirstWednesday2; But that means you define the week start on Monday, and define week 1 as the one which contains at least 5 days. Or maybe this definition is because it then contains the majority of working days in week one? As I am also interested in calendrical stuff  can you find out more about where you got this definition? Is it the one usually used in Netherlands? According to that week website all of the EU should have adopted the ISO at least for official and business use. We can of course do that discussion in email (andy@hoerstemeier.de), it seems to get a bit too much special for this forum. Bye, Andy 
#14




RE: DateToWeek function?
Hi guys,
i just saw Delphi6 on a friends job and it seems borland has added a lot of new date functions to Delphi6. Here's the code to REALLY get the correct weeknumber for any of you interested: procedure DecodeDateWeek(const AValue: TDateTime; out AYear, AWeekOfYear, ADayOfWeek: Word); var LDayOfYear: Integer; LMonth, LDay: Word; LStart: TDateTime; LStartDayOfWeek, LEndDayOfWeek: Word; LLeap: Boolean; begin LLeap := DecodeDateFully(AValue, AYear, LMonth, LDay, ADayOfWeek); ADayOfWeek := CDayMap[ADayOfWeek]; LStart := EncodeDate(AYear, 1, 1); LDayOfYear := Trunc(AValue  LStart + 1); LStartDayOfWeek := DayOfTheWeek(LStart); if LStartDayOfWeek in [DayFriday, DaySaturday, DaySunday] then Dec(LDayOfYear, 8  LStartDayOfWeek) else Inc(LDayOfYear, LStartDayOfWeek  1); if LDayOfYear <= 0 then DecodeDateWeek(LStart  1, AYear, AWeekOfYear, LDay) else begin AWeekOfYear := LDayOfYear div 7; if LDayOfYear mod 7 <> 0 then Inc(AWeekOfYear); if AWeekOfYear > 52 then begin LEndDayOfWeek := LStartDayOfWeek; if LLeap then begin if LEndDayOfWeek = DaySunday then LEndDayOfWeek := DayMonday else Inc(LEndDayOfWeek); end; if LEndDayOfWeek in [DayMonday, DayTuesday, DayWednesday] then begin Inc(AYear); AWeekOfYear := 1; end; end; end; end; For the REALLY REALLY REALLY interested amongst us: ALL date/time routines in Delphi6. CheckSqlTimeStamp procedure Checks whether a TSQLTimeStamp value represents a valid date and time. CompareDate function Indicates the relationship between the date portions of two TDateTime values. CompareDateTime function Indicates the relationship between two TDateTime values. CompareTime function Indicates the relationship between the time portions of two TDateTime values. CurrentYear function Creates a new directory. Date function Returns the current date. DateDelta constant Specifies the difference between TDateTime and TTimeStamp values. DateOf function Strips the time portion from a TDateTime value. DateTimeToFileDate function Converts a TDateTime object to an OS timestamp. DateTimeToSQLTimeStamp function Converts a TDateTime value to a TSQLTimeStamp value. DateTimeToStr function Converts a TDateTime value to a string. DateTimeToString procedure Converts a TDateTime value to a string using a specified Format. DateTimeToSystemTime procedure Converts a TDateTime value into the Win32 API's TSystemTime type. DateTimeToTimeStamp function Converts a TDateTime value into the corresponding TTimeStamp value. DateToStr function Converts a TDateTime value to a string. Day of week constants Provide symbolic constants for ISO 8601compliant day of the week values. DayOf function Returns the day of the month represented by a TDateTime value. DayOfTheMonth function Returns the day of the month represented by a TDateTime value. DayOfTheWeek function Returns the day of the week represented by a TDateTime value. DayOfTheYear function Returns the number of days between a specified TDateTime value and December 31 of the previous year. DayOfWeek function Returns the day of the week for a specified date. DaysBetween function Returns the number of whole days between two specified TDateTime values. DaysInAMonth function Returns the number of days in a specified month of a specified year. DaysInAYear function Returns the number of days in a specified year. DaysInMonth function Returns the number of days in the month of a specified TDateTime value. DaysInYear function Returns the number of days in the year of a specified TDateTime value. DaySpan function Returns the number of days (including fractional days) between two specified TDateTime values. DecodeDate procedure Returns Year, Month, and Day values for a TDateTime value. DecodeDateDay procedure Returns the year and day of the year for a specified TDateTime value. DecodeDateFully function Returns Year, Month, and Day, and DayofWeek values for a TDateTime value. DecodeDateMonthWeek procedure Returns the year, month, week of the month, and day of the week for a specified TDateTime value. DecodeDateTime procedure Returns Year, Month, Day, Hour, Minute, Second, and MilliSecond values for a TDateTime value. DecodeDateWeek procedure Returns the year, week of the year, and day of the week for a specified TDateTime value. DecodeDayOfWeekInMonth procedure For a given TDateTime value, returns the year, month, day of the week, and the count of that day of the week in the month. DecodeTime procedure Breaks a TDateTime value into hours, minutes, seconds, and milliseconds. EncodeDate function; TryEncodeDate function Returns a TDateTime value that represents a specified Year, Month, and Day. EncodeDateDay function Returns a TDateTime value that represents a specified day of the year for a specified year. EncodeDateMonthWeek function Returns a TDateTime objectvalue that represents a specified day of a specified week in a specified month and year. EncodeDateTime function Returns a TDateTime value that represents a specified year, month, day, hour, minute, second, and millisecond. EncodeDateWeek function Returns a TDateTime value that represents a specified day of a specified week in a specified year. EncodeDayOfWeekInMonth function Returns a TDateTime value that represents a specified occurrence of a day of the week within a specified month and year. EncodeTime function;TryEncodeTime function Returns a TDateTime value for a specified Hour, Min, Sec, and MSec. EndOfADay function Returns a TDateTime value that represents the last millisecond of a specified day. EndOfAMonth function Returns a TDateTime value that represents the last millisecond of the last day of a specified month. EndOfAWeek function Returns a TDateTime value that represents the last millisecond of a specified day of a specified week. EndOfAYear function Returns a TDateTime value that represents the last millisecond of a specified year. EndOfTheDay function Returns a TDateTime value that represents the last millisecond of the day identified by a specified TDateTime value. EndOfTheMonth function Returns a TDateTime value that represents the last millisecond of the last day of the month identified by a specified TDateTime value. EndOfTheWeek function Returns a TDateTime value that represents the last millisecond of the last day of the week identified by a specified TDateTime value. EndOfTheYear function Returns a TDateTime value that represents the last millisecond of the last day of the year identified by a specified TDateTime value. FormatDateTime function Formats a TDateTime value. HourOf function Returns the hour of the day represented by a TDateTime value. HourOfTheDay function Returns the hour of the day represented by a TDateTime value. HourOfTheMonth function Returns the number of hours between a specified TDateTime value and 12:00 AM of the first day of the month. HourOfTheWeek function Returns the number of hours between a specified TDateTime value and 12:00 AM of the first day of the week. HourOfTheYear function Returns the number of hours between a specified TDateTime value and 12:00 AM of the first day of the year. HoursBetween function Returns the number of whole hours between two specified TDateTime values. HourSpan function Returns the number of Hours (including fractional Hours) between two specified TDateTime values. HoursPerDay constant;MinsPerDay constant;SecsPerDay constant;MSecsPerDay constant Values useful in time calculations. IncAMonth procedure Increments date data by one month. IncDay function Returns a date shifted by a specified number of days. IncHour function Returns a date/time value shifted by a specified number of hours. IncMilliSecond function Returns a date/time value shifted by a specified number of milliseconds. IncMinute function Returns a date/time value shifted by a specified number of minutes. IncMonth function Returns a date shifted by a specified number of months. IncSecond function Returns a date/time value shifted by a specified number of seconds. IncWeek function Returns a date shifted by a specified number of weeks. IncYear function Returns a date shifted by a specified number of years. IsInLeapYear function Indicates whether a specified TDateTime value occurs in a leap year. IsLeapYear function Indicates whether a specified year is a leap year. IsPM function Indicates whether the time portion of a specified TDateTime value occurs after noon. IsSameDay function Indicates whether a specified TDateTime value occurs on a the same day as a criterion date. IsToday function Indicates whether a specified TDateTime value occurs on the current date. IsValidDate function Indicates whether a specified year, month, and day represent a valid date. IsValidDateDay function Indicates whether a specified year and day of the year represent a valid date. IsValidDateMonthWeek function Indicates whether a specified year, month, week of the month, and day of the week represent a valid date. IsValidDateTime function Indicates whether a specified year, month, day, hour, minute, second, and millisecond represent a valid date and time. IsValidDateWeek function Indicates whether a specified year, week of the year, and day of the week represent a valid date. IsValidTime function Indicates whether a specified hour, minute, second, and millisecond represent a valid date and time. MilliSecondOf function Returns the millisecond of the second represented by a TDateTime value. MilliSecondOfTheDay function Returns the number of milliseconds between a specified TDateTime value and 12:00:00:00 AM of the same day. MilliSecondOfTheHour function Returns the number of milliseconds between a specified TDateTime value and the beginning of the same hour. MilliSecondOfTheMinute function Returns the number of milliseconds between a specified TDateTime value and the beginning of the same minute. MilliSecondOfTheMonth function Returns the number of milliseconds between a specified TDateTime value and 12:00:00:00 AM of the first day of the month. MilliSecondOfTheSecond function Returns the millisecond of the second represented by a TDateTime value. MilliSecondOfTheWeek function Returns the number of milliseconds between a specified TDateTime value and 12:00:00:00 AM of the first day of the week. MilliSecondOfTheYear function Returns the number of milliseconds between a specified TDateTime value and 12:00:00:00 AM of the first day of the year. MilliSecondsBetween function Returns the number of milliseconds between two specified TDateTime values. MilliSecondSpan function Returns the number of milliseconds between two specified TDateTime values. MinuteOf function Returns the minute of the hour represented by a TDateTime value. MinuteOfTheDay function Returns the number of minutes between a specified TDateTime value and 12:00 AM of the same day. MinuteOfTheHour function Returns the number of minutes between a specified TDateTime value and the beginning of the same hour. MinuteOfTheMonth function Returns the number of minutes between a specified TDateTime value and 12:00 AM of the first day of the month. MinuteOfTheWeek function Returns the number of minutes between a specified TDateTime value and 12:00 AM of the first day of the week. MinuteOfTheYear function Returns the number of minutes between a specified TDateTime value and 12:00 AM of the first day of the year. MinutesBetween function Returns the number of minutes between two specified TDateTime values. MinuteSpan function Returns the number of minutes, including fractions thereof, between two specified TDateTime values. MonthOf function Returns the month of the year represented by a TDateTime value. MonthOfTheYear function Returns the month of the year represented by a TDateTime value. MonthsBetween function Returns the approximate number of months between two specified TDateTime values. MonthSpan function Returns the approximate number of months (including fractions thereof) between two specified TDateTime values. MSecsToTimeStamp function Converts a specified number of milliseconds into a TTimeStamp value. Now function Returns the current date and time. NthDayOfWeek function Returns which occurence of its weekday a specified TDateTime value represents. NullSQLTimeStamp constant Represents a NULL TSQLTimeStamp value. RecodeDate function Replaces the date portion of a specified TDateTime value. RecodeDateTime function Selectively replaces parts of a specified TDateTime value. RecodeDay function Replaces the day of the month for a specified TDateTime value. RecodeHour function Replaces the hour of the day for a specified TDateTime value. RecodeLeaveFieldAsIs constant Identifies a parameter to RecodeDateTime that should not be used. RecodeMilliSecond function Replaces the millisecond of the second for a specified TDateTime value. RecodeMinute function Replaces the minute of the hour for a specified TDateTime value. RecodeMonth function Replaces the month of the year for a specified TDateTime value. RecodeSecond function Replaces the second of the minute for a specified TDateTime value. RecodeTime function Replaces the time portion of a specified TDateTime value. RecodeYear function Replaces the year for a specified TDateTime value. ReplaceDate procedure Replaces the date portion of a TDateTime value with a specified date. ReplaceTime procedure Replaces the time portion of a TDateTime value with a specified time. SameDate function Indicates whether two TDateTime values represent the same year, month, and day. SameDateTime function Indicates whether two TDateTime values represent the same year, month, day, hour, minute, second, and millisecond. SameTime function Indicates whether two TDateTime values represent the same time of day, ignoring the date portion. SecondOf function Returns the second of the minute represented by a TDateTime value. SecondOfTheDay function Returns the number of seconds between a specified TDateTime value and 12:00:00 AM of the same day. SecondOfTheHour function Returns the number of seconds between a specified TDateTime value and the beginning of the same hour. SecondOfTheMinute function Returns the number of seconds between a specified TDateTime value and the beginning of the same minute. SecondOfTheMonth function Returns the number of seconds between a specified TDateTime value and 12:00:00 AM on the first day of the month. SecondOfTheWeek function Returns the number of seconds between a specified TDateTime value and 12:00:00 AM of the first day of the week. SecondOfTheYear function Returns the number of seconds between a specified TDateTime value and 12:00:00 AM on the first day of the year. SecondsBetween function Returns the number of seconds between two specified TDateTime values. SecondSpan function Returns the number of seconds (including fractions thereof) between two specified TDateTime values. SQLDayOfWeek function Indicates the day of the week when a specified TSQLTimeStamp value occurs. SQLTimeStampToDateTime function Converts a TSQLTimeStamp value to a TDateTime value. SQLTimeStampToStr function Converts a TSQLTimeStamp value to a string. StartOfADay function Returns a TDateTime value that represents 12:00:00:00 AM on a specified day. StartOfAMonth function Returns a TDateTime value that represents 12:00:00:00 AM on the first day of a specified month. StartOfAWeek function Returns a TDateTime value that represents 12:00:00:00 AM on a specified day of a specified week. StartOfAYear function Returns a TDateTime value that represents 12:00:00:00 AM on the first day of a specified year. StartOfTheDay function Returns a TDateTime value that represents 12:00:00:00 AM on the day identified by a specified TDateTime value. StartOfTheMonth function Returns a TDateTime value that represents 12:00:00:00 AM on the first day of the month identified by a specified TDateTime value. StartOfTheWeek function Returns a TDateTime value that represents 12:00:00:00 AM on the first day of the week identified by a specified TDateTime value. StartOfTheYear function Returns a TDateTime value that represents 12:00:00:00 AM on the first day of the year identified by a specified TDateTime value. StrToDate function Converts a string to a TDateTime value. StrToDateTime function Converts a string to a TDateTime value. StrToSQLTimeStamp function Converts a string to a TSQLTimeStamp value. StrToTime function Converts a string to a TDateTime value. SystemTimeToDateTime function Converts a _SYSTEMTIMETSystemTime value into a TDateTime objectvalue. Time function Returns the current time. TimeOf function Strips the date portion from a TDateTime value. TimeStampToDateTime function Converts a TTimeStamp value into the corresponding TDateTime value. TimeStampToMSecs function Converts a TTimeStamp value into an absolute number of milliseconds. TimeToStr function Returns a string that represents a TDateTime value. Today function Returns a TDateTime value that represents the current date. Tomorrow function Returns a TDateTime value that represents the following day. TryEncodeDateDay function Calculates the TDateTime value that represents a specified day of the year for a specified year. TryEncodeDateMonthWeek function Calculates the TDateTime value that represents a specified day of a specified week in a specified month and year. TryEncodeDateTime function Calculates the TDateTime value that represents a specified year, month, day, hour, minute, second, and millisecond. TryEncodeDateWeek function Calculates the TDateTime value that represents a specified day of a specified week in a specified year. TryEncodeDayOfWeekInMonth function Calculates a TDateTime value that represents a specified occurrence of a day of the week within a specified month and year. TryRecodeDateTime function Selectively replaces parts of a specified TDateTime value. TryStrToSQLTimeStamp function Converts a string to a TSQLTimeStamp value. UnixDateDelta constant Specifies the difference between TDateTime and TIME_T values. WeekOf function Returns the week of the year represented by a TDateTime value. WeekOfTheMonth function Returns the week of the month represented by a TDateTime value. WeekOfTheYear function Returns the week of the year represented by a TDateTime value. WeeksBetween function Returns the number of whole weeks between two specified TDateTime values. WeeksInAYear function Returns the number of weeks in a specified year. WeeksInYear function Returns the number of weeks in the year of a specified TDateTime value. WeekSpan function Returns the number of weeks (including fractional weeks) between two specified TDateTime values. WithinPastDays function Indicates whether two dates are within a specified number of days of each other. WithinPastHours function Indicates whether two date/time values are within a specified number of hours of each other. WithinPastMilliSeconds function Indicates whether two date/time values are within a specified number of milliseconds of each other. WithinPastMinutes function Indicates whether two date/time values are within a specified number of minutes of each other. WithinPastMonths function Indicates whether two date/time values are within a specified number of months of each other. WithinPastSeconds function Indicates whether two date/time values are within a specified number of seconds of each other. WithinPastWeeks function Indicates whether two date/time values are within a specified number of weeks of each other. WithinPastYears function Indicates whether two date/time values are within a specified number of years of each other. YearOf function Returns the year represented by a TDateTime value. YearsBetween function Returns the approximate number of years between two specified TDateTime values. YearSpan function Returns the approximate number of years (including fractions thereof) between two specified TDateTime values. Yesterday function Returns a TDateTime value that represents the preceding day. Greetz, ::TeD On The NeT:: =[ QUESTIONS ]========================================= don't forget to accept this reply if it helped you! ================================================== ===== =[ EXTRA INFO ]======================================== Location : Netherlands (GMT +01.00) Languages : English, Dutch, German Age : 25 ICQ : Be smart and figure out what my UIN is... ================================================== ===== 
Thread Tools  
Display Modes  

