How to read only Formatdatatime numbers

Asked

Viewed 69 times

0

I needed to know the current time of the computer and compare with values in the database, example:

'agenda.hora >= "' + formatdatatime('hh:ss',now) + ";

It turns out that both in the bank and the formate will return me a string, that of the bank I can convert to integer, but I do not know how to convert the result of the FormatDataTime.

3 answers

1

Using Unit Dateutils, you have access to functions such as Hourof(Tdatetime). You can do

valorHora:=HourOf(now);

and use this value as an int

1

I will give the example of two ways to do it, in the first use the function FormatDataTime, example:

var VHora, VMinuto: integer;
Begin
  VHora := StrToInt(FormatDataTime('hh',now));  
  VMinuto := StrToInt(FormatDataTime('nn',now));
End; 

The second way as mentioned by @Tiagorodrigues, we will use the function HourOf and MinuteOf, example:

var VHora, VMinuto: integer;
Begin
  VHora := HourOf(now); 
  VMinuto := MinuteOf(now);
End; 

The question is using only hours and minutes, if you need seconds let me know that I edit the answer. Any more questions let me know.

0

There is a function called Decodedatetime, that uses Unit Dateutils and it breaks the date and time parts, so you can work with each element. Follow an example.

procedure TForm2.Button3Click(Sender: TObject);
var
  wAno, wMes, wDia : Word;
  wHora, wMinuto, wSegundos, wMilissegundo : Word;
  iHour, iMinuto: Integer;
begin
  DecodeDateTime(Now(), wAno, wMes, wDia, wHora, wMinuto, wSegundos, wMilissegundo);

  iHour := wHora;
  iMinuto := wMinuto;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.