Board index » cppbuilder » Need help for TDateTime (to show the daytime...)

Need help for TDateTime (to show the daytime...)

hi dear builders,

I need help again to display daytimes via TDateTime (like "good morning...")

The following function should solve it...

void __fastcall TForm1::Daytime(TObject *Sender)
{
   Label1->Caption = txtDateMsg->Text + Date().FormatString("dddd, \"der\" d
mmmm, yyyy");
   TDateTime TimeNow    = StrToTime(Time()),
                     Morning      = StrToTime("06:00:00"),
                     Afternoon   = StrToTime("12:00:00"),
                     Night           = StrToTime("18:00:00"),
                     AfterNight   = StrToTime("22:00:00");

   if(Morning     > TimeNow.CurrentTime()) ShowMessage("Good Morning");
   if(Afternoon   > TimeNow.CurrentTime()) ShowMessage("Good Afternoon");
   if(Night         > TimeNow.CurrentTime()) ShowMessage("Good Night");
   if(AfterNight  > TimeNow.CurrentTime()) ShowMessage("Good AfterNight");

Quote
}

Well...the function seems to work well (no errors...) but I believe there is
a nicer way to
handle the daytimes...or ?

Have someone a idea...?

Oren

 

Re:Need help for TDateTime (to show the daytime...)


Quote
<O...@bzffm.de> wrote:
>I need help again to display daytimes via TDateTime (like "good morning...")
...
>Well...the function seems to work well (no errors...) but I believe there is
>a nicer way to handle the daytimes...or ?

You can take the fractional part of a double that represents a
TDateTime. Something like this:

#include <math.h>

// ...

double Ignore;
double TimeFraction = modf(Now(), &Ignore);

AnsiString Message = "Good ";

if(TimeFraction < 0.25)
  Message += "night";
else if(TimeFraction < 0.5)
  Message += "morning";
else if(TimeFraction < 0.75)
  Message += "afternoon";
else
  Message += "evening";

ShowMessage(Message);

Take care,

Hens Zimmerman

Re:Need help for TDateTime (to show the daytime...)


You could use theDecodeTime() function in order to get the individual hour,
then do your comparison:

unsigned short hour, minute, seconds, milliseconds;
AnsiString message;

Time().DecodeTime(&hour, &minute, &seconds, &milliseconds);

if(hour > 22) message = "Good AfterNight";
else if(hour > 18) message = "Good Night";
else if(hour > 12) message = "Good Afternoon";
else message = "Good Morning";

ShowMessage(message);

Gambit

Quote
<O...@bzffm.de> wrote in message news:931v3u$9eb8@bornews.inprise.com...
> Well...the function seems to work well (no errors...) but I believe there
is
> a nicer way to
> handle the daytimes...or ?

Re:Need help for TDateTime (to show the daytime...)


Thank you very much guys, now it works like I wanted :-)

Oren

Other Threads