Go Premium for a chance to win a PS4. Enter to Win

x
Solved

delphi DayOfYear

Posted on 2010-09-06
Medium Priority
697 Views
i need to convert DayOfYear ---  eg. 249 to str 9/6/2010
0
Question by:arwar49
• 3
• 3
• 2

Author Comment

ID: 33614900
this should be pretty straightforward - i just have a coder's block at the moment.
0

Author Comment

ID: 33614909
i guess the 2010 is not part of DayOfYear - i said i had coder' s block

so i need function to convert 249 (DOY) to 9/6 (mo/day)
0

LVL 58

Expert Comment

ID: 33614940
Obviously you need the year, because 249 is different when it is a leap year and when not, right??
0

LVL 58

Accepted Solution

cyberkiwi earned 200 total points
ID: 33614951
function DayOfYear(dayNum: integer; year: Word = 0): TDateTime;
var
m,d: Word;
begin
if year = 0 then
DecodeDate(Now, year,m,d);
Result := EncodeDate(year,1,1)-1+dayNum;
end;

ShowMessage(FormatDateTime('yyyy-mm-dd', DayOfYear(249)));
0

Author Closing Comment

ID: 33614978
ok
0

LVL 38

Expert Comment

ID: 33615229
why not look in the DateUtils unit next time ?

There is the incday function which should be the easiest

Result := IncDay(EncodeDate(year, 1, 1), daynum-1);
0

LVL 58

Expert Comment

ID: 33621771

Result := IncDay(EncodeDate(year, 1, 1), daynum-1);

different to my line

Result := EncodeDate(year,1,1)-1+dayNum;

which skips the function and uses floating point register directly.
The other content of the function is only to default to current year.

Unless there is some magic to IncDay that I am not aware of?
0

LVL 38

Expert Comment

ID: 33624395
it is not different cyberkiwi,
i was more responding to forgetting things and having blackouts (usually monday morning)

the best to thing then is to look in those units for some unblocking ideas
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether youâ€™re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Starting up a Project
Suggested Courses
Course of the Month10 days, 20 hours left to enroll