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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Of This Day Last Year.

Status
Not open for further replies.

jlaw

Programmer
Nov 3, 2001
3
GB
How can I calculate the date of this day last year
 
What do you mean by this day last year, exactly? Does the following do what you want?

procedure TForm1.Button1Click(Sender: TObject);
var
Year, Month, Day: Word;
begin
DecodeDate(Date, Year, Month, Day);
Edit1.Text := DateToStr(EncodeDate(Year-1 ,Month, Day));
end;
 
Hi jlaw

This wont help if you are doing some sort of college project, but this is probobaly the simplist way to do it.
Use a Datetime picker component for the W32 pallet.
according to the help file it should be possible to get the day of week directly from the component!! but I cant find it so.
This works by reseting the datetime picker calender back 1 year,reading the new date, then using the dayofweek function to index an array of day's.

If you need to show that you can do the calcs, get hold of 'A crash course in Pascal' can't remember the authour.
there is a nice formula and example in the book.


var
Form1: TForm1;
const days: array [1..7] of string =('Sunday','Monday','Teusday','Wednesday','Thursday','Friday','Saturday');

implementation


{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var ndate: Tdate;
year,mth,day: word;
begin
dtp.DateFormat := dflong;
decodedate(now, year,mth,day);
dec(year);dtp.Date := encodedate(year,mth,day);
edit1.text := days[dayofweek(dtp.date)];
end;

end.

Hope it helps Steve.
 

var y,m,d: word;
todaylastyear: TDateTime;
dayofweeklastyear: integer;
begin
decodeDate(now, y,m,d);
todaylastyear := encodeDate(y-1,m,d);
dayofweeklastyear := DayOfWeek(todaylastyear);
{1..7 = sunday..saturday}
end;
TealWren
 
Oops
Quite right Tealwren you don't need the datetimepicker.
My first thought was that it would know the day of the week in any year, and it got left in somehow!!

Looking again at the original question I am not sure if that is what he wanted though !!
Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top