Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Working with Dates in WMI
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working with Dates in WMI
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-02-09

    Table of Contents:
  • Working with Dates in WMI
  • The Datetime format
  • Converting VBScript dates to WMI Dates
  • Converting WMI dates to VBScript Dates
  • Building Queries with Dates

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Dates in WMI - Converting WMI dates to VBScript Dates


    (Page 4 of 5 )

    You will also need to be able to convert WMI’s datetime values into VBScript’s Date values.  There are two approaches to this.  The first is obvious.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

     

    For Each objOperatingSystem In colOperatingSystems

        WScript.Echo DatetimeToDate(objOperatingSystem.InstallDate)

    Next

     

    Function DatetimeToDate(strDate)

       DatetimeToDate = _

           CDate(Mid(strDate, 5, 2) & _

           "/" & _

           Mid(strDate, 7, 2) & _

           "/" & _

           Left(strDate, 4) & _

           " " & _

           Mid (strDate, 9, 2) & _

           ":" & _

           Mid(strDate, 11, 2) & _

           ":" & _

           Mid(strDate, 13, 2))

    End Function

    This example again returns the date on which the current operating system was installed.  This time, the value is being converted to a VT_Date compatible string.  Since WMI’s DateTime format is nothing more than a formatted string, VBScript’s string functions can be used to extract the necessary parts.  Here, the DatetimeToDate function takes a datetime string such as “20080101000000.000000-300” and converts it to a string like “01/01/2008 00:00:00.”  This string is then converted to a VBScript Date value using the CDate function.

    Function DatetimeToDate(strUTC)

       Set datetime = CreateObject("WbemScripting.SWbemDateTime")

       datetime.Value = strUTC

     

       DatetimeToDate = datetime.GetVarDate(True)

    End Function

    A simpler version of this function could be constructed using the WMI COM object I showed you previously.  In this case, the datetime value is used to set the SWbemDateTime object’s Value property directly.  Then the GetVarDate method is used to return the VBS-compatible Date.  Notice that this function accepts a single Boolean value that indicates whether the returned date is considered local or returned in GMT (adjusted for the offset).

    More Windows Scripting Articles
    More By Nilpo


     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek