?
Solved

Days since the beginning of the year

Posted on 1999-07-08
7
Medium Priority
?
696 Views
Last Modified: 2010-04-06
In Delphi 3, how can I determine the number of days that have passed since the beginning of the year?

For example, on July 4, 185 days had passed since the beginning of the year.

0
Comment
Question by:Tom Knowlton
  • 4
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
KE earned 200 total points
ID: 1387284
Trunc( Now - EncodeDate( 1999, 1, 1 ) )+1;
Where Now is a TDateTime that you want to calculate !
Fill in appropriate year - fx. by Decoding the "Now" date !

0
 
LVL 3

Expert Comment

by:KE
ID: 1387285
Like this:

Function DaysSince( aDateTime: TDateTime ) : Integer;
var
  d, m, y : Word;
begin
  DecodeDate( aDateTime, y, m, d );
  Result := Trunc( aDateTime - EncodeDate( y, 1, 1 ) )+1
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage( IntToStr( DaysSince( EncodeDate( 1999, 7, 4 ) ) ) );
end;


0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 1387286
Perfect!
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 1387287
This was my final code:

procedure TFormDaysPassed.FormCreate(Sender: TObject);
begin
  daysPassedNum := Trunc( Now - EncodeDate( 1999, 1, 1 ) )+1;
  DecodeDate(Now, Year, Month, Day);
  LabelDate.Caption := LabelDate.Caption +
                    '  ' +
         IntToStr(Month) +
                     ' / ' +
           IntToStr(Day) +
                     ' / ' +
           IntToStr(Year);

   LabelDaysPassed.Caption := LabelDaysPassed.Caption +
                                                 '  ' +
                               IntToStr(daysPassedNum);
end;
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 1387288
Actually, this code is more flexible...will work for year 2000, 2001, etc.:

//daysPassedNum := Trunc( Now - EncodeDate( Year, 1, 1 ) ) + 1;

begin
  DecodeDate(Now, Year, Month, Day);
  daysPassedNum := Trunc( Now - EncodeDate( Year, 1, 1 ) )+ 1;
  LabelDate.Caption := LabelDate.Caption +
                    '  ' +
         IntToStr(Month) +
                     ' / ' +
           IntToStr(Day) +
                     ' / ' +
           IntToStr(Year);

   LabelDaysPassed.Caption := LabelDaysPassed.Caption +
                                                 '  ' +
                               IntToStr(daysPassedNum);
end;
0
 
LVL 3

Expert Comment

by:KE
ID: 1387289
Yep, you'll have to consider leap days/years.

Thanks for the nice grade :-)

PS. Try these nice things also:
FormatDateTime('dd/mm/yyyy', aDateTime);
x.Caption := Format('%s %d', [x.Caption, DaysPassed] );

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 1387290
I'll have to try those out some time.

Thanks again.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question