Solved

Variant and TDateTime

Posted on 2009-05-13
8
2,178 Views
Last Modified: 2012-06-27
Hi, how i can check il a "Variant" Parameter or Variable is a TDateTime or a Double?
0
Comment
Question by:ISIGest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372779
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24373427
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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24373433
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!

 

Author Comment

by:ISIGest
ID: 24383057
The VarType "varDouble" could not be a valid TDateTime but a normal Double value.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24383199
how did you get the value ?
that will determine your approach !
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24383388
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
 

Author Comment

by:ISIGest
ID: 24393259
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
 
LVL 12

Accepted Solution

by:
Hypo earned 500 total points
ID: 24393620
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question