Recent

Author Topic: [SOLVED] Date - very easy question, but "ugly" solution  (Read 757 times)

Nicole

  • Hero Member
  • *****
  • Posts: 970
[SOLVED] Date - very easy question, but "ugly" solution
« on: January 31, 2023, 07:36:43 pm »
What I want to do is:

DateTimePicker_ExitZeit.Time:=now;

Sure, this works very quickly.
If it is 15:29:25 - this value will be written into the TimePicker.

To avoid quick-reading-errors, I want the seconds to be gone.
To write
15:29:00

or 15:30:00

even better

( or 15:30 - may this work at all? it must stay a picker)

So how to do this:
I can get it by MinuteOf(now) and HourOf(now) as word types.
Then I figure them out and convert them back to time.

This is UGLY.
Has anybody a nicer way?
« Last Edit: February 01, 2023, 08:09:11 pm by Nicole »

dsiders

  • Hero Member
  • *****
  • Posts: 1045
Re: Date - very easy question, but "ugly" solution
« Reply #1 on: January 31, 2023, 08:20:05 pm »
What I want to do is:

DateTimePicker_ExitZeit.Time:=now;

Sure, this works very quickly.
If it is 15:29:25 - this value will be written into the TimePicker.

To avoid quick-reading-errors, I want the seconds to be gone.
To write
15:29:00

or 15:30:00

even better

( or 15:30 - may this work at all? it must stay a picker)

So how to do this:
I can get it by MinuteOf(now) and HourOf(now) as word types.
Then I figure them out and convert them back to time.

This is UGLY.
Has anybody a nicer way?

To zero out Seconds and Milliseconds:

Code: Pascal  [Select][+][-]
  1. AWhen  := Now;
  2. ADateTimePicker.Time := RecodeTime(AWhen, HourOf(AWhen), MinuteOf(AWhen), 0, 0);
  3.  

To format the time on the date time picker:

Code: Pascal  [Select][+][-]
  1. ADateTimePicker.TimeDisplay := tdHM;
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Date – very easy question, but “ugly” solution
« Reply #2 on: January 31, 2023, 08:53:51 pm »
[…] Has anybody a nicer way?
Probably. How about this:
Code: Pascal  [Select][+][-]
  1. t := int(t * 1440) / 1440;
Yours Sincerely
Kai Burghardt

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: Date - very easy question, but "ugly" solution
« Reply #3 on: February 01, 2023, 07:09:15 pm »
Thank you for the answers.

I would prefer a solution like
tdHM

- if I could!

I read, "invalid key" and am offer only display suggestions, which make sense only if the kind would be "date". Yes, the "kind" is set to dktime.

The wanted display is
HH:MM

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Date - very easy question, but "ugly" solution
« Reply #4 on: February 01, 2023, 07:17:11 pm »
I don't understand. This is working for me (It can be set in the Object Inspector as well):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DateTimePicker := TDateTimePicker.Create(self);
  4.   with DateTimePicker do
  5.   begin
  6.     Kind := dtkTime;
  7.     TimeDisplay := tdHM;
  8.     Parent := Self;
  9.   end;
  10. end;

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: Date - very easy question, but "ugly" solution
« Reply #5 on: February 01, 2023, 08:08:52 pm »
Wow, interesting.
I tried it in the ObjectInspector.

After having copied the line into the AfterConstruction-Event, - it works for me as well.

Thank you, now the solution is BEAUTIFUL.

 

TinyPortal © 2005-2018