VBS Date Calculator App

by ezman in Circuits > Software

5850 Views, 9 Favorites, 0 Comments

VBS Date Calculator App

Scr1.bmp
Introduction
Have you ever wondered what the date and day of the week it would be in X number of days from today?  How many days there are from today to some future date?  How about the number of days between two dates? 

Lets look at some practical applications for a Date Calculator App.vbs:
What is the mature date of Fantastic tomatoes if they say 72 days? - I can not wait to eat a Fantastic tomato.
How many days until Flag Day? - Flag Day is an under recognized observance.
How many days from next pay day to vacation? - How many days do I have to save money for vacation?

Wouldn't you like to have an app like that?  Wait, wouldn't you like to create an app like that?  You could customize your own Date Calculator App.vbs

The Story
My wife will ask me what day of the week a certain date will be.  My kids ask me how long until vacation.  This morning my daughter asked me how many days until ......  I responded, "I do not know ,,,,,,,,,,,,,,,,,  I am going to create a program so you can answer the question yourself."   She thought that was, "Cooool."

Well, I started to think on it later.  The more I thought on it; the more I wanted to create this application.  I sat down at my computer and thought about what it should do, how should it look, how much time do I want to put into this application?  With these questions in my focus I also thought about other things.   I wanted the application to be something simple to use, something with easy to understand code and not a resource hog.  After a little more thinking this is what I came up with, read on to create your own Date Calculator App.vbs.

Because this is .vbs (Visual Basic Scripting) you will need a Windows based Operating System.

Objective:
The intent of this instructable is to
1)  Create a simple application capable of performing 3 different calculations.
2)  Share code to move beyond the "Hello World" message box to a dynamic interactive application.
3)  Motivate you to create other useful applications by showing how easy the code can be.

Items Covered

ex.bmp
In this instructable you will see:
1)  An Input box,
          An Input box is a way to respond to a question.  It could be something like entering a password or choosing between multiple options, as in the case before you today.
2)  A Message box,
          A Message box is a way to display a message and allow you to answer Yes, No, Cancel, Retry or Ignore.
3)  Select Case Statements,
          Select Case Statements tells the code what to do based upon the users input.
4)  Handle Errors and
          Error Handling is important when you move beyond the basic "Hello World" message box and start to have choices.  No one wants to see a popup error script.  Sometimes they are cryptic even to a seasoned programmer.
5)  How to Comment out notes in the code.
          Comment out notes is important.  It allows anyone to know what is going on at that point in the code.  The code will not run when the line is preceded by ' an Apostrophe.

Skill Level
Easy – if you copy and paste. 
Medium – if you want to change the code.

Time to Complete
5 – 10 minutes, more if you make changes.

Warning:  If you make the wrong change to the aforementioned code.  You could, at worst, ruin your day.   It is no fun chasing errors.  What I have learned is that it is usually a simple mistake.
Disclaimer:  Modify at your own risk.

What Is the Date and Day in X Days?

opt1.bmp
This is what you will see when you enter "1" into the 1st Input box.

Count the Number of Days

opt2.bmp
This is what you will see when you enter "2" into the 1st Input box.

How Many Days Between 2 Dates?

opt3.bmp
This is what you will see when you enter "3" into the 1st Input box.

Error Handling

e1.bmp
This is what you will see when you enter something outside of the given parameters.

Error Handling is important when you move beyond the basic "Hello World" message box and start to have choices.  No one wants to see a popup error script.  Sometimes they are cryptic even to a seasoned programmer. (Notation) I am not a seasoned programmer.

The Code

Code.bmp
1)  Open the Notepad application:
Click Start => click Programs => click Accessories => click Notepad
Or

Click Start => Click Run => Type Notepad in the Run input box then Click OK.

2)  Copy the code below the Apostrophe and Asterisks line then Paste it into Notepad

' *********************************************

Dim Notice
Dim Message
Dim Message1
Dim Message2

'  Error Handling
On Error Resume Next

'  Standard Inputbox
Notice = "  My Date Counter "
Message = "What would you like to do ?" & vbcr & vbcr &_
"1 - Date from Today by Number of Days " & vbcr &_
"2 - Number of Days to Future Date " & vbcr &_
"3 - Number of Days between 2 Dates" & vbcr & vbcr &_
"Enter the number of your choice."

'  InputBox results
Question = InputBox(message,Notice)
'  Check for Null or empty inputbox then cancels
IF IsEmpty(Question) THEN
WScript.quit()

ELSEIF Len(Question) = 0 THEN
WScript.quit()

ELSEIF Question = 0 THEN
WScript.quit()

ELSE
  SELECT Case Question
   Case 1 Run(1)
   Case 2 Run(2)
   Case 3 Run(3)
  END SELECT

END IF

'  Case Statements for result from Inputbox
Sub Run(var)
Set WS = CreateObject("WScript.shell")
    Select Case var
Case 1 Message=inputbox("How many days do you want to count? " &  vbCrLf &  vbCrLf & "Either positive or negative number.", "  Enter the # of Days ")
    Message2=weekdayname(weekday(DateAdd("d", message, Date))) & ", " & DateAdd("d", Message, Date)
    Message = Message & " days from today will be " &  vbCrLf & "    " &  Message2
     
Case 2 Message1=inputbox("How many days until ?" &  vbCrLf &  vbCrLf & "Enter the Date as m/d/yy or mm/dd/yyyy", "  Enter Future Date ")
    Message2=DateDiff("d", Date , Message1)
    Message="There are " & Message2 & " days until " &  vbCrLf & "      " & Message1
     
Case 3 Message1=inputbox("Start Date ? " &  vbCrLf &  vbCrLf & "Enter the Date as m/d/yy or mm/dd/yyyy", "  Date From")
    Message=inputbox("End Date ?  " &  vbCrLf &  vbCrLf & "Enter the Date as m/d/yy or mm/dd/yyyy", "  Date To")
    Message2=DateDiff("d", Message1 , Message)
    Message="There are " & Message2 & " Days " &  vbCrLf & " From " & Message1 & vbCrLf & " To " & Message

    END SELECT
End Sub

'  Error handling message
IF Err.Number <> 0 THEN
msgbox "You entered something incorrectly.  Try again.  ",0+16," Ooopps...."
WScript.quit()
END IF

'  Final results
Msgbox Message, vbInformation, Notice

Save the File

Save2.bmp
1)  Click File,
2)  Click Save,
3)  Choose the location where to Save this file,
4)  Change Save as type: from Text Documents (*.txt) to "All Files",
5)  Give the file a name i.e. DateCounter.vbs,
6)  Click Save.

Congratulations you are done.

Observations & Summary

Scr1.bmp
Warning:  If you make the wrong change to the aforementioned code.  You could, at worst, ruin your day.  It is no fun chasing errors.  What I have learned is that it is usually a simple mistake.
Disclaimer:  Modify at your own risk.

Observations
1)  It is easy to use.
2)  It is a small file and it sits on my desktop.
3)  It can be easily modified to do a lot of actions.
4)  You can enter a negative ( - ) number as well as the results displaying as a negative ( - ) amount.  You can count and display a past date.  i.e. Today is 4/20/2013.  How many days ago was Income Taxes file date 4/15/2013.  The response would be, "There are -5 days until 4/15/2013".
5)  This is a .vbs (Visual Basic Scripting) file you will need to be running Windows Operating System.
6)  If it does not work.  Then recopy the aforementioned code and paste it into the Notepad application then save the file with the extension .vbs.

Summary
This is a handy little app.  I wanted to keep adding functionality, but I will let you customize it, use your creativeness.  I am satisfied with the results. 

Hear advice, and receive instruction, so that you may be wise in your latter end.