I\'m trying to read the install date from WMI (Win32_OperatingSystem.InstallDate
). The return value looks like this: 20091020221246.000000+180. How can I get a
System.Management.ManagementDateTimeConverter.ToDateTime
WbemScripting.SWbemDateTime does not always work. Better way:
function WmiDate2DT (S: string; var UtcOffset: integer): TDateTime ;
// yyyymmddhhnnss.zzzzzzsUUU +60 means 60 mins of UTC time
// 20030709091030.686000+060
// 1234567890123456789012345
var
yy, mm, dd, hh, nn, ss, zz: integer ;
timeDT: TDateTime ;
function GetNum (offset, len: integer): integer ;
var
E: Integer;
begin
Val (copy (S, offset, len), result, E) ;
end ;
begin
result := 0 ;
UtcOffset := 0 ;
if length (S) <> 25 then exit ; // fixed length
yy := GetNum (1, 4) ;
mm := GetNum (5, 2) ;
if (mm = 0) or (mm > 12) then exit ;
dd := GetNum (7, 2) ;
if (dd = 0) or (dd > 31) then exit ;
if NOT TryEncodeDate (yy, mm, dd, result) then // D6 and later
begin
result := -1 ;
exit ;
end ;
hh := GetNum (9, 2) ;
nn := GetNum (11, 2) ;
ss := GetNum (13, 2) ;
zz := 0 ;
if Length (S) >= 18 then zz := GetNum (16, 3) ;
if NOT TryEncodeTime (hh, nn, ss, zz, timeDT) then exit ; // D6 and later
result := result + timeDT ;
UtcOffset := GetNum (22, 4) ; // including sign
end ;
function VarDateToDateTime(const V: OleVariant): TDateTime;
var
rawdate: string ;
utcoffset: integer ;
begin
Result:=0;
if VarIsNull(V) then exit;
Dt.Value := V;
try
Result:=Dt.GetVarDate;
except
rawdate:=V;
result := WmiDate2DT (rawdate, utcoffset);
end;
end;
MagWMI from Magenta Systems contains MagWmiDate2DT() that does this.
http://www.magsys.co.uk/delphi/magwmi.asp
maybe this helps: http://technet.microsoft.com/en-us/library/ee156576.aspx
Instead of parsing and extracting the values manually (how the accepted answer suggest), you can use the WbemScripting.SWbemDateTime object.
check this sample
function WmiDateToTDatetime(vDate : OleVariant) : TDateTime;
var
FWbemDateObj : OleVariant;
begin;
FWbemDateObj := CreateOleObject('WbemScripting.SWbemDateTime');
FWbemDateObj.Value:=vDate;
Result:=FWbemDateObj.GetVarDate;
end;
For more info about this topic you can read this artile WMI Tasks using Delphi – Dates and Times