Name

VarToDateTime Function

Syntax

function VarToDateTime(const V: Variant): TDateTime;

Description

VarToDateTime converts a Variant to a TDateTime. If the Variant cannot be converted to varDate, the function reports runtime error 15 (EVariantError).

VarToDateTime is a real function.

Tips and Tricks

  • Assigning a Variant to a TDateTime variable does not work. Because TDateTime is a floating-point type, the assignment would try to convert the Variant to a varDouble, not a varDate.

  • VarToDateTime eventually calls the Windows API function VariantChangeTypeEx to parse the string and interpret the date and time. See the Platform SDK documentation for details. VarToDateTime accepts many more formats than StrToDateTime.

Example

// Parse a string as a date and time.
function StringToDateTime(const S: string): TDateTime;
var
  V: Variant;
begin
  V := S;
  Result := VarToDateTime(V);
end;

See Also

TDateTime Type, VarFromDateTime Function, Variant Type

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.