Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Days since the beginning of the year

Posted on 1999-07-08
7
Medium Priority
?
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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