Ver Mensaje Individual
  #5  
Antiguo 05-09-2013
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
dtomeysoto,

Cita:
Empezado por dtomeysoto
...me gustaría mostrar...la duración de la sesión, es decir cuanto tiempo estuvo abierta la sesión (horas, minutos, segundos y milisegundos)...
Revisa este código basado en la solución sugerida en el Msg #2:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DateUtils;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  InicioSesion, FinSesion : TDateTime;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   InicioSesion := Now;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   YYI, MMI, DDI, HHI, NNI, SSI, MSI : Word;
   YYF, MMF, DDF, HHF, NNF, SSF, MSF : Word;
   YYT, MMT, DDT, HHT, NNT, SST, MST : Word;
   FinalSesion : String;

begin

   FinSesion  := Now;

   ShortDateFormat := 'dd/mm/yyyy';
   ShortTimeFormat := 'hh:nn:ss:zzz';
   DateSeparator := '/';
   TimeSeparator := ':';

   DecodeDateTime(InicioSesion,YYI,MMI,DDI,HHI,NNI,SSI,MSI);
   DecodeDateTime(FinSesion,YYF,MMF,DDF,HHF,NNF,SSF,MSF);

   YYT := YYF - YYI;

   if MMF >= MMI then
   begin
      MMT := MMF - MMI
   end
   else
   begin
      YYF := YYF - 1;
      MMF := MMF + 12 - MMI;
   end;

   if DDF >= DDI then
   begin
      DDT := DDF - DDI
   end
   else
   begin
      MMT := MMT - 1;
      DDT := DDF + MonthDays[IsLeapYear(YYF),MMF] - DDI;
   end;

   if MMT < 0 then
   begin
      YYT := YYT - 1;
      MMT := 12 + MMT;
   end;

   if HHF >= HHI then
   begin
      HHT := HHF - HHI;
   end
   else
   begin
      DDT := DDT - 1;
      HHT := HHF + 24 - HHI;
   end;

   if NNF >= NNI then
   begin
      NNT := NNF - NNI;
   end
   else
   begin
      HHT := HHT - 1;
      NNT := NNF + 60 - NNI;
   end;

   if HHT < 0 then
   begin
      DDT := DDT - 1;
      HHT := HHT + 24
   end;

   if SSF >= SSI then
   begin
      SST := SSF - SSI;
   end
   else
   begin
      NNT := NNT - 1;
      SST := SSF + 60 - SSI;
   end;

   if MSF >= MSI then
   begin
      MST := MSF - MSI;
   end
   else
   begin
      SST := SST - 1;
      MST := MSF + 1000 - MSI;
   end;

   if SST < 0 then
   begin
      NNT := NNT - 1;
      SST := SST + 60;
   end;

   Memo1.Font.Name := 'Courier';
   with Memo1.Lines do
   begin
      Clear;
      Add('Inicio sesión : ' + DateTimeToStr(InicioSesion));
      Add('Fin sesión    : ' + DateTimeToStr(FinSesion));
      Add('');
      Add('Tiempo transcurrido');
      Add('-------------------');
      Add('Años     : ' + IntToStr(YYT));
      Add('Meses    : ' + IntToStr(MMT));
      Add('Dias     : ' + IntToStr(DDT));
      Add('Horas    : ' + IntToStr(HHT));
      Add('Minutos  : ' + IntToStr(NNT));
      Add('Segundos : ' + IntToStr(SST));
      Add('Miliseg. : ' + IntToStr(MST));
   end;

end;

end.
El código anterior permite mostrar en un componente TMemo el lapso de tiempo transcurridos entre dos variables TDateTime.

Nota: Variable TDateTime Inicial deber ser menor o igual a Variable TDateTime Final.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 05-09-2013 a las 02:13:58.
Responder Con Cita