Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Building Calendars with VBScript Date Func...
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

Building Calendars with VBScript Date Functions
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-01-26

    Table of Contents:
  • Building Calendars with VBScript Date Functions
  • Beginning the script
  • Finding the necessary dates
  • Calculating the remaining values

  • 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


    Building Calendars with VBScript Date Functions - Calculating the remaining values


    (Page 4 of 4 )

    Next we need to calculate some more values.  Let’s think for a minute about the types of values that are actually needed to create a calendar.  First, you obviously need a month and year—which we have.  Next, you need to know how many days are in that month.  Then, you need to know on which day of the week the first appears.  Finally, although it’s not completely necessary, it’s also helpful to know on what weekday the last day of the month falls.

    dteFirstDay = DateSerial(intYear, intMonth, 1)

    intFirstWeekDay = WeekDay(dteFirstDay)

    intDaysInMonth = DaysInMonth(intMonth, intYear)

    intLastWeekDay = WeekDay(DateSerial(intYear, intMonth, intDaysInMonth))

    VBScript’s DateSerial function allows us to easily return a Date type for the first day of the month using the string parts that we have already.  Supplying a day value of 1 obviously returns the first of that month.  The WeekDay function quickly tells us on what weekday that occurs.

    We’ll create a simple function called DaysInMonth to determine the number of days in a month.  Quite simply, it finds the last day of the month.  As it turns out, this is an Integer representation of the number of days in a month.  We can then use this value to return a Date for the last day of the month.  Let’s take a quick look at that function.

    Function DaysInMonth(intMonth, intYear)

       If intMonth = 12 Then

           dteNextMonth = DateSerial(intYear + 1, 1, 1)

       Else

           dteNextMonth = DateSerial(intYear, intMonth + 1, 1)

       End If

     

       DaysInMonth = DateDiff("d", dteFirstDay, dteNextMonth)

    End Function

    This function very simply finds the first day of the following month by incrementing the month value.  Subtracting a single day from this number will return the last day of the month we are working with.  Notice that the following month for December is actually January in the next year.  A simple If statement can handle this case.  If the supplied month is 12, or December, I increment that year instead and use January 1.

    I then use the DateDiff function to return the integer number of days between the first day of the current month and the first day of the following month.  You could just as easily do this using the DateAdd function.

    DaysInMonth = Day(DateAdd("d", -1, dteNextMonth))

    I could have used the DateAdd function to move the first day of the following month back a single day making it the last day of the current month.  The Day function could then be used to return that Integer value.  You can use whichever method you like.

    If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then

        strPath = WScript.ScriptFullName

        strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34)

        CreateObject("WScript.Shell").Run(strCommand)

        WScript.Quit

    End If

     

    strDate = InputBox("Please enter a month and year as MM/YYYY.", "Microsoft Scripting Games 2008")

    If strDate = "" Then WScript.Quit

     

    arrDateParts = Split(strDate, "/")

    intMonth = arrDateParts(0)

    intYear = arrDateParts(1)

     

    dteFirstDay = DateSerial(intYear, intMonth, 1)

    intFirstWeekDay = WeekDay(dteFirstDay)

    intDaysInMonth = DaysInMonth(intMonth, intYear)

    intLastWeekDay = WeekDay(DateSerial(intYear, intMonth, intDaysInMonth))

     

    Function DaysInMonth(intMonth, intYear)

       If intMonth = 12 Then

           dteNextMonth = DateSerial(intYear + 1, 1, 1)

       Else

           dteNextMonth = DateSerial(intYear, intMonth + 1, 1)

       End If

     

       DaysInMonth = DateDiff("d", dteFirstDay, dteNextMonth)

    End Function

    I’m out of space for this article.  At this point, our script looks like this.  We’ve begun our calendar script, calculated all of the necessary values, and now we’re ready to begin building our script’s output display.  We’ll jump right into that in my next article.

    If you want to get an early start, you can download the full code for this article here.  Until next time, coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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