Recent

Author Topic: I need help I'm writing from turkey  (Read 11614 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: I need help I'm writing from turkey
« Reply #15 on: October 24, 2014, 06:23:12 am »
This is ok:
Code: [Select]
s2:=day-day2;

But this one:
Code: [Select]
s3:=(years-years2)*365;
It does not account for leap years (years with 366 days)
so s3 should become:
Code: [Select]
s3 := (years-years2)*365 + LeapYearsOf(years) - LeapYearsOf(years2);

There are two problems with this:
Code: [Select]
s4:=(mounth-mounth2)*30;
First problem, February is:
28 in normal years
29 in leap years
Second problem, there are 7 months with 31 days.

Simplify the problem by dividing it into small simple problems, like:
How to find out if a year is a leap year:
Leap years happen every four years, but not if the year is a multiple of 100 unless it is also a multiple of 400 as well.
Code: [Select]
  IsLeapYear := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));

How many days in full months (or complete months) since the beginning of the year?
The number of full months is equal to Month-1 (or ay - 1 )

Use a small array:
Code: [Select]
//Days in normal years
  MonthDays: array[1..12] of integer =
    (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
and a loop from 1 to (Month - 1) or (ay - 1):
Code: [Select]
    DaysOfCompleteMonths := 0;
    if ay>1 then
      for I := 1 to ay - 1 do Inc(DaysOfCompleteMonths, MonthDays[I])
and if ay>2 and it is a leap year, then add 1:
Code: [Select]
    if ( ay>2 ) and IsLeapYear( yil ) then
      Inc(DaysOfCompleteMonths, 1);
This makes s4:
Code: [Select]
  s4 := DaysOfCompleteMonths(yil, ay) - DaysOfCompleteMonths(yil2, ay2);

To calculate LeapYearsOf(Year) to correct  s3, use:
Code: [Select]
  LeapYearsOf := Year div 4 - Year div 100 + Year div 400;

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: I need help I'm writing from turkey
« Reply #16 on: October 24, 2014, 07:30:07 am »
Merhaba,

Bu tür işlemleri tabiki kendiniz de yapabilirsiniz. Ancak benim tavsiyem "uses" bloğunda "DateUtils" unit'ini kullanmanızdır.

Bu unite içerisinde ise DaysBetween komutunu kullanabilirsiniz.

Örnek bir kod:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := inttostr( DaysBetween(DateEdit2.Date,DateEdit1.Date) );
end;

İyi çalışmalar,


-----------------------------------------------------------------------------------------------

For English users:
Hello,

Ofcourse you may use your functions for this kind operations, but my advise is include DateUtils unit into uses block.
In this unit there is a command that DaysBetween you can use as sample code:


Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := inttostr( DaysBetween(DateEdit2.Date,DateEdit1.Date) );
end;

Good luck,





Merhaba ben sizinki olmadı ben bu şeklide yaptım ancak bütün ayları 30 gün olarak hesaplattım sadece tek istediğim şubat ayının 28 çekme ve diğer ayların 30 veya 31 çekme olaylarını hesap Benim kodum;
Code: [Select]
uses crt;
var
s2,s3,s4,day,day2,mounth,mounth2,years,years2,results:integer;
begin
write('Please enter the day of 1 Chronicles:'); readln(day);
write('1 Chronicles Enter the Month:'); readln(mounth);
write('Please enter Year 1 Chronicles:'); readln(years);
write('Please enter the day of 2 Chronicles:'); readln(day2);
write('2 Chronicles Enter the Month:'); readln(mounth2);
write('Please enter Year 1 Chronicles:'); readln(years2);
clrscr;
s2:=day-day2;
s3:=(years-years2)*365;
s4:=(mounth-mounth2)*30;
result:=s2+s3+s4;
writeln('Days :  ',result);
readln;
end.

Calculate how many days passed for each date, and find the difference:
Code: [Select]
  DaysPassed1 := CalcDaysPassed(gun1, ay1, yil1);
  DaysPassed2 := CalcDaysPassed(gun2, ay2, yil2);
  Answer := abs(DaysPassed1 - DaysPassed2);

How to calculate how many days passed?
1-You have "yil-1" complete years
2-And ay-1 complete months (if ay>1)
3-And gun days
Code: [Select]
DaysPassed := DaysOfCompleteYears + DaysOfCompleteMonths + gun;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: I need help I'm writing from turkey
« Reply #17 on: October 24, 2014, 07:49:24 am »
my advise is include DateUtils

I believe this is an assignment for some course. He said:
I'm not allowed to use the library dateutils library or similar

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: I need help I'm writing from turkey
« Reply #18 on: October 24, 2014, 08:00:11 am »
Ok. that is no problem he can write again the datetutils.



my advise is include DateUtils

I believe this is an assignment for some course. He said:
I'm not allowed to use the library dateutils library or similar

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: I need help I'm writing from turkey
« Reply #19 on: October 24, 2014, 08:26:33 am »
Anyway, Delphi is computing correctly the difference (i.e. date<1900 and date>1900). In fact, its compute is much more simple; that's probably why it's correct.
Ehrm, would be nice to have a bugtracker issue with a demo program... perhaps it's been solved in trunk otherwise it obviously needs to be fixed...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

kmalpha

  • New Member
  • *
  • Posts: 27
Re: I need help I'm writing from turkey
« Reply #20 on: October 25, 2014, 04:09:34 am »
Functions as dateutils be used.konu güncel

 

TinyPortal © 2005-2018