Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 VBScript dates to WMI Dates


    (Page 3 of 5 )

     

    VBScript dates must first be converted to WMI’s datetime format if you wish to use them in a query.  This can be done easily by building a function that serves this purpose.  However, WMI’s COM interface provides a class specifically for working with date and time conversions.

    dtmDate = #January 1, 2008#

     

    Set dateTime = CreateObject("WbemScripting.SWbemDateTime")

     

    dateTime.SetVarDate(dtmDate, True)

     

    WScript.Echo "CIM datetime " & dateTime

    WScript.Echo "Local datetime " & dateTime.GetVarDate()

    The WbemScripting object’s SWbemDateTime class returns a date or time as a datetime value.  By creating an instance of this object class, you can easily prepare dates and times for use in WMI queries.  Since the SWbemDateTime class’s default method is Value, a reference to the object will return the datetime value without specifying the actual Value property.

    CIM datetime 20080101000000.000000-300
    Local datetime 1/1/2008

    The SetVarDate method is used to set the datetime value by providing a VBScript date.  It accepts a VBScript Date value as its parameter.  A second, optional parameter is a Boolean value that indicates whether the time is a local time or GMT time.  This is the easiest way to get a date from VBScript into WMI datetime format.  You are not, however, limited to this conversion.  The SWbemDateTime class has several other useful methods as well for returning date and time portions.

    dtmDate = Now()

     

    Set dateTime = CreateObject("WbemScripting.SWbemDateTime")

     

    dateTime.SetVarDate(Now)

     

    WScript.Echo "Year:" & dateTime.Year

    WScript.Echo "Month:" & dateTime.Month

    WScript.Echo "Day:" & dateTime.Day

    WScript.Echo "Hours:" & dateTime.Hours

    WScript.Echo "Minutes:" & dateTime.Minutes

    WScript.Echo "Seconds:" & dateTime.Seconds

    WScript.Echo "Microseconds:" & dateTime.Microseconds

    WScript.Echo "UTC Offset:" & dateTime.UTC

    The example above enumerates each of the properties that the SWbemDateTime class provides for returning date and time portions.  These are self explanatory. 

    Year:2008
    Month:9
    Day:10
    Hours:23
    Minutes:48
    Seconds:22
    Microseconds:0
    UTC Offset:-240

    Here you see that the UTC offset is -240.  Earlier, on this same system the offset was -300.  What does that mean?  The Eastern Time Zone in the United States observes Daylight Savings Time.  Each spring our clock moves ahead one hour, reducing the UTC offset by 60 minutes.  Thus, the current offset is only -240 when daylight savings time is being observed.

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek