Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Julian date 1

Status
Not open for further replies.

awarnica

Programmer
Jan 16, 2003
78
CA
Hi all,

Im using Delphi 5 and need a function that will give me the following for the date:

"day_of_year" a space then a 2 digit year.
Today would be 23 02, and Feb 1st 2003 would be 32 03

I can handle the year and space part (duh), but I cant find a function to return the day of the year correctly (incorporating leap years etc...)

Thanks in advance,

Andrew
 
OK better question for you all, what would be wrong this function:

procedure TfrmMain.Button1Click(Sender: TObject);
var
Year, Month, Day: Word;
iTotal: integer;
begin
DecodeDate(StrToDate(Edit1.Text), Year, Month, Day);

Case month of
1: iTotal := 0;
2: iTotal := 31;
3: iTotal := 59;
4: iTotal := 90;
5: iTotal := 120;
6: iTotal := 151;
7: iTotal := 181;
8: iTotal := 212;
9: iTotal := 243;
10: iTotal := 273;
11: iTotal := 304;
12: iTotal := 334;
13: iTotal := 365;
end;

if IsLeapYear(Year) AND (month > 2) then
iTotal := iTotal + 1;

iTotal := iTotal + Day;

Edit2.Text := IntToStr(iTotal);
end;

Thanks
 
Could you do something like this:

procedure TfrmMain.Button1Click(Sender: TObject);
var
Year, Month, Day: Word;
iTotal: integer;
LDate : TDateTime;
begin
LDate := StrToDate(Edit1.Text);
DecodeDate(LDate, Year, Month, Day);
Edit2.Text := IntToStr(Trunc(LDate - EncodeDate(Year,1,1)));
end;


Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group." - tag line I stole
 
Nice and simple, looks good, Ill give it a shot.

Thanks.
 
Made a slight adjustment, I was getting a partial day but rounded and adjusted for the time of day:

function FindDayOfYear(dDate: TDateTime): string;
var
Year, Month, Day: Word;
dTotal, dRounded: double;
begin
DecodeDate(dDate, Year, Month, Day);
dRounded := Round(dDate);
if dRounded < dDate then
dRounded := dRounded + 1;
dTotal := dRounded - EncodeDate(Year,1,1);
Result := FloatToStr(dTotal) + ' ' + IntToStr(Year);
end;

Seems to work now, thanks a bundle!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top