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!

Urgently need help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hello,

I'm working on a prototype project to accept any date in the calendar and display the corresponding day of the week for this date.
Th text displayed should indicate if the date entered was in the past or the present or the future. E.g. the 12th February 2002 is a Tuesday.

thanks
 
If you need to work a lower level:-
The dayofweek() function will get you a numeric value represnting the day. Use this to index an array of strings holding the day names.
You need to make sure the date entered can be converted to a valid TDatetime value (with Strtodate) then pass this to day of week.

I knocked this up last week to remind my boss to go to the bank you Might find some clues in it.

:)

unit Pay;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type Days = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);

type
TPayDay = class(TForm)
Button1: TButton;
Display: TLabel;
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function DayNumToday(Dn: integer): Days;
function Post(D: integer): string;
{ Public declarations }
end;

var
PayDay: TPayDay;

const daynames: array[Sunday..Saturday] of string = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');

implementation

{$R *.DFM}

procedure TPayDay.FormActivate(Sender: TObject);
var Present: TDateTime;
Year, Month, DayNumber: Word;
Day: Days;
begin
Day := DayNumToDay(DayofWeek(Now));
if Day in [Monday..Friday] then
begin
Present:= Now;
DecodeDate(Present, Year, Month, DayNumber);
case Daynumber of
21: if Day in [Wednesday,Thursday,Friday] then
Display.Visible := true;
22: if day = Wednesday then
Display.Visible := true;
23: if day in [Monday,Tuesday,Wednesday] then
Display.Visible := true;

else Application.Terminate;

end;
end
else
Application.Terminate;

end;

function TPayDay.DayNumToday(Dn: integer): Days;
begin
case Dn of
1: Result := Sunday;
2: Result := Monday;
3: Result := Tuesday;
4: Result := Wednesday;
5: Result := Thursday;
6: Result := Friday;
7: Result := Saturday;
end;
end;

function TPayDay.Post(D: integer): string;
begin
case D of
1,21,31: Result := 'st';
2,22: Result := 'nd';
else Result := 'th';
end;
end;

procedure TPayDay.Button1Click(Sender: TObject);
begin
close;
end;

end.


Steve

 
Steve,

Just to add to the mix a bit, you may not need your Days type. Consider, if you will this bit of code:

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
   dtInput : TDateTime;

begin

   dtInput := strToDate( edit1.text );
   inputBox( 'Day Name Demo', Edit1.Text  + ' was a: ',
             formatDateTime( 'dddd', dtInput ) ); 

end;

In other words, the formatDateTime() function (in SysUtils) can return the name of the day for you.

Also, I think you need another case in TPayDay.Post(). For example:

Code:
function TPayDay.Post(D: integer): string;
   begin
      case D of
         1,21,31: Result := 'st';
         2,22:    Result := 'nd';
         3,23:    Result := 'rd';
      else        Result := 'th';
      end;
  end;

This should help your boss make sense of the "3th" or "23th". :)

Hope this helps...

-- Lance
 
Cheers Lance

My original notes had the 'rd' case but it diddn't find its way into the program, I did say this was knocked together!!!
In fact it dosent actualy print out the day names at all (I removed the de-bugging lines that did) but it demonstrates use of basic types for a newcomer.
I had a feeeling that there was a way to obtain the day names but cound remember the function, so thanks for that.

Steve.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top