Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Diferencia de minutos entre dos TDateTime (https://www.clubdelphi.com/foros/showthread.php?t=75119)

jlrdz 29-07-2011 19:04:12

Diferencia de minutos entre dos TDateTime
 
Un saludo primero que nada, tengo un problema al sacar la diferencia entre 2 variables TDateTime, almaceno en 2 variables tiempo1, tiempo2 y almaceno la diferencia en otra variable TDateTime.

Código Delphi [-]
tiempo1:=StrToDateTime('29/07/2011 12:08:00');
tiempo2:=StrToDateTime('29/07/2011 13:10:00');

//he probado con estas funciones pero ninguna me ha resultado y lo que está comentado es el resultado que arrojan cuando hago.

      diferencia:=MinuteSpan(tiempo1,tiempo2);
      showmessage(DateTimeToStr(diferencia));
      //  01/03/1900 11:59:59 PM
      diferencia:=MinutesBetween(tiempo1,tiempo2);
      showmessage(DateTimeToStr(diferencia));
      //  01/03/1900
      diferencia:=tiempo1-tiempo12;
      showmessage(DateTimeToStr(diferencia));
       //30/12/1899  1:02:00 AM

La verdad es que ya busqué en bastantes hilos de este foro y he quedado más confundido, espero que puedan ayudarme y de antemano muchas gracias.

maeyanes 29-07-2011 19:06:08

Hola...

¿Ya verificaste bien cual es el tipo de dato que devuelven esas funciones? Por que según creo recordar, devuelven tipos enteros y no tipos TDateTime.


Saludos...

jlrdz 29-07-2011 19:10:17

De hecho me interesa recuperarlo como entero para saber si hay una diferencia menor a 10 minutos pero la verdad soy nuevo en esto y estaba investigando pero me he confundido más :s. Saludos!.

maeyanes 29-07-2011 19:18:04

Hola...

Me imagino que revisaste en la ayuda de Delphi:

Cita:

Empezado por Delphi Help
From DateUtils.pas

Código Delphi [-]
function MinuteSpan(const ANow: TDateTime; const AThen: TDateTime): Double;

Description
Returns the number of minutes, including fractions thereof, between two specified TDateTime values.

Call MinuteSpan to obtain the difference, in minutes, between two TDateTime values. Unlike the MinutesBetween function, which only counts entire minutes, MinuteSpan reports incomplete minutes as a fraction of an entire minute.

Si te fijas, el valor devuelto por la función es del tipo Double, no TDateTime, esto es, que la forma correcta de mostrarlo sería:

Código Delphi [-]
ShowMessage(FloatToStr(Diferencia));  // Donde Diferencia: Double;

Si usas la función MinutesBetween, el valor devuelto es un Int64 y la forma de mostrarlo sería:

Código Delphi [-]
ShowMessage(IntToStr(Diferencia));  // Donde Diferencia: Integer;



Saludos...

jlrdz 29-07-2011 19:27:43

Cierto, ya he visualizado bien el resultado y muchísimas gracias por su ayuda, el error estaba en mí. Saludos.

Chris 29-07-2011 19:28:46

Cita:

Empezado por jlrdz (Mensaje 407924)
De hecho me interesa recuperarlo como entero para saber si hay una diferencia menor a 10 minutos pero la verdad soy nuevo en esto y estaba investigando pero me he confundido más :s. Saludos!.

Talvez solamente necesitas esto:
Código Delphi [-]
if MinuteSpan(tiempo1,tiempo2) > 10 then
begin
    // ....
end;

Saludos,
Chris

PD.: Ten cuidado con la función StrToDateTime. Según la ayuda de Delphi:

Cita:

Empezado por Ayuda de Delphi
Converts a string to a TDateTime value.

Pascal
function StrToDateTime(const S: string): TDateTime; overload;
function StrToDateTime(const S: string; const FormatSettings: TFormatSettings): TDateTime; overload;

File
SysUtils

Description
Call StrToDateTime to parse a string that specifies a date and time value. If S does not contain a valid date, StrToDateTime raises an EConvertError exception.

The S parameter must use the current locale's date/time format. In the US, this is commonly MM/DD/YY HH:MM:SS format. Specifying AM or PM as part of the time is optional, as are the seconds. Use 24-hour time (7:45 PM is entered as 19:45, for example) if AM or PM is not specified.

Year values between 0 and 99 are converted using the TwoDigitYearCenturyWindow. This value is stored either in a global variable (first form) or as a field in the FormatSettings parameter (second form) See "Currency and Date-Time Formatting Variables" for more information.

The first form of StrToDateTime is not thread-safe, because it uses localization information contained in global variables. The second form of StrToDateTime, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of StrToDateTime, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call GetLocaleFormatSettings.



La franja horaria es GMT +2. Ahora son las 12:30:45.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi