Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Working With Dates and Times in VBScript
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 and Times in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-01-20

    Table of Contents:
  • Working With Dates and Times in VBScript
  • Retrieving the date and time
  • Working with date and time parts
  • Assigning Date values
  • Performing calculations
  • Date formats

  • 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 and Times in VBScript - Working with date and time parts


    (Page 3 of 6 )

    You may not always wish to return the entire date or time.  Your script may only need to know the current month or day of the week.  As a result, VBScript provides a number of different functions for retrieving date and time parts.

    dtmValue = Now()

     

    WScript.Echo Hour(dtmValue)

    WScript.Echo Minute(dtmValue)

    WScript.Echo Second(dtmValue)

     

    Output:

    0

    59

    1

    The most commonly used of these functions return portions of the current time.  The Hour function returns a number between 0 and 24 that represents the current hour.  Note that this number is independent of whether or not the system uses a 12 or 24-hour clock.  The Minute and Second functions both return a value between 0 and 60, as you might expect.  The Second function always returns a two-digit number, so it may contain a leading zero.

    WScript.Echo Day(dtmValue)

    WScript.Echo Month(dtmValue)

    WScript.Echo MonthName(Month(dtmValue))

    WScript.Echo Year(dtmValue)

     

    Output:

    5

    9

    September

    2008

    Similar functions exist for extracting parts of a date.  The Day function returns a number between 1 and 31, indicating the day of the month; the Month function returns a number between 1 and 12, which represents the month of the year; and the Year function returns a number indicating the four-digit year.  VBScript also provides the MonthName function for returning the textual month name.  This function accepts a single parameter—an integer indicating the month.  It returns a string that varies based upon the local system language.

    WScript.Echo Weekday(dtmValue)

    WScript.Echo WeekdayName(Weekday(dtmValue))

     

    Output:

    6

    Friday

    VBScript can also determine the weekday for a specified date.  The Weekday function returns a number between 1 and 7 that indicates the current day of the week.  The WeekdayName function can use this number to return the textual representation of that value.  This string varies depending upon the current system language and local regional and time settings, which determine what day of the week is first on the calendar.

    WScript.Echo DatePart("y", Now)

    WScript.Echo DatePart("ww", Now)

     

    Output:

    249

    36

    Finally, VBScript provides another very interesting function.  The DatePart function is used to return parts of a date, much like the functions you’ve just seen.  It differs in its flexibility.  It accepts two required parameters.  The first is a string that indicates what interval (or date part) to return.  The list of acceptable values can be found in Table 1 below.  The second required parameter is a Date value.  In the example above, I’ve used date part to return the number of days that have passed in the current year and the number of weeks, respectively.

    Table 1: Date interval strings

    “d”

    Day

    “m”

    Month

    “yyyy”

    Year

    “h”

    Hour

    “n”

    Minute

    “s”

    Second

    “w”

    Weekday

    “q”

    Quarter

    “y”

    Day of Year

    “ww”

    Week of Year

    DatePart also accepts two optional parameters.  The first of these determines the day to be considered the first day of the week.  If used, it accepts a constant value from Table 2 below.  If omitted, this value defaults to vbSunday.

    Table 2: VBScript WeekDay Constants

    Constant

    Value

    Description

    vbUseSystemDayOfWeek

    0

    Use NLS API (system) setting

    vbSunday

    1

    Sunday

    vbMonday

    2

    Monday

    vbTuesday

    3

    Tuesday

    vbWednesday

    4

    Wednesday

    vbThursday

    5

    Thursday

    vbFriday

    6

    Friday

    vbSaturday

    7

    Saturday

    DatePart optionally accepts a final parameter that specifies the first week of the year.  It accepts a constant value from Table 3 below.  If omitted, this defaults to vbFirstJan1.

    Table 3: VBScript FirstWeekOfYear Constants

    Constant

    Value

    Description

    vbUseSystem

    0

    Use NLS API (system) setting

    vbFirstJan1

    1

    The week in which Jan 1 occurs.

    vbFirstFourDays

    2

    The first week having at least 4 days in the new year.

    vbFirstFullWeek

    3

    The first full week of the new year.

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