How to convert WMI DateTime to standard DateTime date?

I am trying to read the installation date from WMI ( Win32_OperatingSystem.InstallDate ). The return value is as follows: 20091020221246.000000 + 180. How can I get a valid date?

+4
source share
5 answers

Instead of parsing and retrieving the values ​​manually (as the accepted answer suggests), 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 information on this topic, you can read this article on WMI Tasks using Delphi – Dates and Times

+5
source

MagWMI from Magenta Systems contains MagWmiDate2DT (), which does this.

http://www.magsys.co.uk/delphi/magwmi.asp

+3
source
 System.Management.ManagementDateTimeConverter.ToDateTime 
+1
source

WbemScripting.SWbemDateTime does not always work. The best 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; 
+1
source

All Articles