Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2369
  • Last Modified:

Variant and TDateTime

Hi, how i can check il a "Variant" Parameter or Variable is a TDateTime or a Double?
0
ISIGest
Asked:
ISIGest
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Shanmuga SundaramCommented:
0
 
HypoCommented:
That link didn't work for me... anyway, you can check what type a variant holds, by using the VarType function... see the example below.

regards
Hypo
procedure TForm1.Button1Click(Sender: TObject);
var aVar : Variant;
    aDateTime : TDateTime;
    aDouble : Double;
begin
  aVar := aDateTime;
  Case VarType(aVar) of
    varDate: ShowMessage('Variant is a date');
    varDouble: ShowMessage('Variant is a double');
    else ShowMessage('Variant is of some other type');
  end;
 
  aVar := aDouble;
  Case VarType(aVar) of
    varDate: ShowMessage('Variant is a date');
    varDouble: ShowMessage('Variant is a double');
    else ShowMessage('Variant is of some other type');
  end;
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
case VarType(Param) of
  varDate: ShowMessage('Date : ' + FormatDateTime('dd/mm/yyyy hh:nn:ss', VarToDateTime(Param)));
  varDouble: ShowMessage('Double: ' + FloatToStr(Param));
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ISIGestAuthor Commented:
The VarType "varDouble" could not be a valid TDateTime but a normal Double value.
0
 
Geert GruwezOracle dbaCommented:
how did you get the value ?
that will determine your approach !
0
 
HypoCommented:
ISIGest: Sorry, but I really don't understand your comment... It didn't work for you?  you got a runtime error? Can you post some of your code, with an explaination of what you are trying to do, and what results you'd expect if it was working?
0
 
ISIGestAuthor Commented:
Ok...sorry...
If I test the variant variable with a VarType() function, the result can be varDouble if the variant value is a TDateTime like "15/05/2009 10.34" (in Double value format = 39948,440278), but it can be also a normal Double value like 350,25 or other value like 8540,69.
Ok, now how i can check if this value is a "valid" Double for a valid TDateTime?

Thanks.
0
 
HypoCommented:
Ok,
so your question is actually how to identify that a double contains a value that is a valid datetime?

The problem is this, a TDateTime is in the sense of CPU, the same thing as a double, and that's the reason why the delphi compiler allows you to type myDateTime := myDouble; and vice versa. Any valid Double can also be a valid TDateTime.

So, If the code that wrote the datetime to the Variant varaible, wrote it as a double variable, then the Variant type will contain the type info varDouble, and only if the code that wrote the datetime was using a TDateTime variable, only then will the Variant contain the type info varDate.

The only things I can come up with for you to solve this are,
1:
If you have access to the code that's writing to the Variant variable, then you should make sure that it always uses a variable of type TDateTime when setting the Variants type.

2:
If you don't have access to that code, then you could perform an extra test on all Variants that you'd expect to be a datetime, but that is of type varDouble, to see that they are in va valid range. The current dates lay somewhere around 39000, each day is a new integer, so if 39000.xxx is today, then 39001 is tomorrow etc etc... If you have a range of dates that you consider to be valid, like from "Jan 1st, 1900" to "Dec 31th, 2099", then you can rule out alot of the doubles that don't contain valid dates, just by checking if the value is within that intervall. (see example below). It's not perfect, but I don't think you can get a perfect sollution for that problem, since a datetime is in some sense the same thing as a double.

regards
Hypo
Function IsValidDateTime(AVariant : Variant) : Boolean;
begin
  Case VarType(AVariant) of
    varDate: Result := True;
    varDouble: Result := (AVariant > EncodeDate(1900, 1, 1)) and (AVariant < EncodeDate(2100, 1, 1));
    else Result := False;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var A : Variant;
    B : Double;
begin
  A := Now;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
 
  B := 1/2;
  A := B;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
 
  B := Now;
  A := B;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
end;

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now