Solved

Help!! DateTimePicker

Posted on 2004-10-28
1,768 Views
Last Modified: 2012-08-13
HI experts,

I have tried this for 3 hours and couldn't get it to work. :((
Scenerio:


1)I have a comboBox which I could choose the time interval in an hour of 15, 20, 30 or 60 Mins.

2)I have a dateTimePicker(shows only time) for schedule items.
The increment or decrement of the DateTimePicker minute is according to the interval chosen.
if interval of 20 is choosen, when I click up sign, 10:20 PM becomes 10:40PM, down button 10:20PM becomes 10:00PM.

3)  Schedule items (FYI) are scheduled according to the interval chosen. if interval of 30 is chosen, the schedule can only be the time interval 30. Eg 10.30pm, 11.30pm, 3.30AM.
But some schedule items are schedule before the interval change. Therefore my schedule item time would not have a interval time of the chosen interval. EG. 10.30PM but the interval chosen is 20 minutes.

What I want to do is when I click the up or down button of the date time picker it will go to the nearest time of the current chosen interval. For example.

--------------------------------------------------------
Example 1)
Interval Chosen := 30;
Original Time := 10.20PM; <- the time in datetimepicker

When I click up                  when i click down
once > 10:30 pm               once > 10:00 PM
twice > 11.00pm                Twice > 9:30 PM

and so on
--------------------------------------------------
Example 2)
Interval Chosen := 20 minutes;
Original Time := 10.45PM;  <- the time in datetimepicker

When I click up                  when i click down
once > 11:00 pm               once > 10:40 PM
twice > 11.20 pm               Twice > 10:20 PM

and so on

--------------------------------------------------
Example 2)
Interval Chosen := 20 minutes;
Original Time := 10.15PM;  <- the time in datetimepicker

When I click up                  when i click down
once > 10:20 pm               once > 10:00 PM
twice > 10.40 pm               Twice > 9:40 PM

and so on

-------------------------------------------------------------------
I'm a student just started learning delphi this year, so please bear with me :)
Below are some of my codes, which are not very good. Just ignore my codes and see if there are any good idea.
The alogrithms of my codes is wrong. It works for some scenerio but not all. And some parts of the codes are not in there.

Thank you very much in advance. I really need it to work ASAP. Thanks.





//************************************************************
procedure TDefaultPlannerItemEditForm.DateTimePickerChange(
  Sender: TObject);
var
  TempMin, TempHour, DTHour, DTMin, Ohour, Omin, tempsec, tempmsec : Word;
  tInterval, tempTime, ainterval : TTime;
  Minus, Unequal : Boolean;
  a, b, c, CurrentInterval : integer;
begin
  CurrentInterval := FormSchedule.SLInterval;
  TempMin :=  CurrentInterval mod MintuesPerHour;
  TempHour := CurrentInterval div MintuesPerHour;

  tInterval := EncodeTime(TempHour, TempMin, 0, 0);

 if sender = StartTimeDateTimePicker then
    tempTime := OriginalStartTime
  else
    tempTime := OriginalEndTime;

  DecodeTime((sender as TDateTimePicker).Time, DTHour, DTMin, tempsec, tempmsec);
  DecodeTime(tempTime, Ohour, Omin, tempsec, tempmsec);

  if (Omin <> 0) and
    (Max(CurrentInterval, Omin) mod
       Min(CurrentInterval, Omin) <> 0 ) then
  begin
    a := Omin - CurrentInterval;
    b := CurrentInterval - Omin;
    c := CurrentInterval - a;
  end;

  //let temptime and datetimepicker date be the same before comparing the time
  (sender as TDateTimePicker).Date := tempTime;

  Minus := tempTime > (sender as TDateTimePicker).time;
  if DTHour  = Ohour then
  begin
    Unequal := False;
    if (Omin <> 0) and (Max(CurrentInterval, Omin) mod
         Min(CurrentInterval, Omin) <> 0 ) then
    begin
      a := Omin - CurrentInterval;
      b := CurrentInterval - Omin;
      c := Max(CurrentInterval, a) - Min(CurrentInterval, a);
      Unequal := True;
    end;



    if Minus then
    begin
      if Unequal then
      begin
        if CurrentInterval > Omin then
          (sender as TDateTimePicker).Time := tempTime - EncodeTime(0,b*2,0,0)
        else if CurrentInterval < Omin then
          (sender as TDateTimePicker).Time := tempTime - EncodeTime(0,a,0,0);
      end
      else
        (sender as TDateTimePicker).Time := tempTime - tInterval;
    end
    else
    begin
      if Unequal then
      begin
        if CurrentInterval > Omin then
          (sender as TDateTimePicker).Time := tempTime + EncodeTime(0,b,0,0)
        else if CurrentInterval < Omin then
          (sender as TDateTimePicker).Time := tempTime + EncodeTime(0,c,0,0);
      end
      else
        (sender as TDateTimePicker).Time :=
          tempTime + ifthen (DTMin <> 59, tInterval, -tInterval );
    end;
  end;

  tempTime :=  (sender as TDateTimePicker).Time;

  if sender = StartTimeDateTimePicker then
    OriginalStartTime := tempTime
  else
    OriginalEndTime := tempTime;
0
Question by:AgeOfWars
    2 Comments
     
    LVL 7

    Accepted Solution

    by:
    I know this sounds like your homework, but it's interesting to me, so I make a few comments.

    Here's one way to do it. Once you have the time broken down into HOUR and MINUTE, call this routine to do the adjustment. It can only adjust for an interval of 0 to 60 minutes.

    Your specifications seem confusing. If the interval is 30, do you want 10:30, 11:00, 11:30?  or only 10:30, 11:30?

    This routine performs "multiples" if the interval is less than 31, and forces the interval past the hour otherwise.
    If you don't want the multiples, you can take it out, or add a parameter that specifies when to do it.


    procedure AdjustToInterval(aUp:boolean; aIntMins:integer; var aHour,aMin:integer);
    var
      Diff:integer;
    begin
      { Adjust hour/min to the next interval }
      { Intervals of 0 minutes or less and 61 or more are treated as 60 minutes }
      { Intervals 30 minutes or less are forced to a multiple of aIntMins }
      { Intervals 31 or more, are forced to aIntMins past the hour }

      if (aIntMins<=0) or (60<aIntMins) then
        aIntMins:=60;
      if aIntMins>30 then begin
        aIntMins:=aIntMins mod 60;
        if aMin>=aIntMins then begin
          {Force to next/prev hour}
          aMin:=aIntMins;
          if aUp then
            inc(aHour)
          else
            dec(aHour);
        end
        else begin
          {Force to current/prev hour}
          aMin:=aIntMins;
          if not aUp then
            dec(aHour);
        end;
      end
      else begin
        {Interval less than 31 minutes}
        { How far from interval are the minutes? }
        Diff:=aMin mod aIntMins;
        if Diff=0 then begin
          Diff:=aIntMins; {we go a complete interval}
        end
        else begin
          { Going to "next" interval }
          if aUp then
            Diff:=aIntMins-Diff; {subtract for up}
        end;

        if aUp then begin
          aMin:=aMin+Diff;
        end
        else {down} begin
          aMin:=aMin-Diff;
        end;
      end;

      { Sanity checks }
      while aMin>=60 do begin
        dec(aMin,60);
        inc(aHour);
        if aHour>=24 then
          aHour:=aHour-24;
      end;
      while aMin<0 do begin
        inc(aMin,60);
        dec(aHour);
        if aHour<0 then
          aHour:=aHour+24;
      end;
    end;
    0
     

    Author Comment

    by:AgeOfWars
    Hey LRHGuy,

    It works! You are a genius to think of the algorithm.

    I just added a line to dec the Hour when (aUp is False) and dec interval is changes the hour.

    Thanks a lot !!!!
    Regards,
    Ian


    ------------------------------Con't from
        if aUp then begin
          aMin:=aMin+Diff;
        end
        else {down} begin
          if aMin = 59 then                 <----------------------
            dec(ahour);
          aMin:=aMin-Diff;
        end;
      end;

      { Sanity checks }
      while aMin>=60 do begin
        dec(aMin,60);
        inc(aHour);
        if aHour>=24 then
          aHour:=aHour-24;

    -------------------------------------------
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now