VBScript Date Functions: Date Format, DateAdd, and cDate Functions

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 9, 2024

Introduction to VBScript Date Functions: VBScript Tutorial #8

In my previous tutorial, we discussed ‘VBS Arrays‘. This is part of our in-depth VBScript learning series.

In this tutorial, I will explain more about ‘Date Functionsthat are used in the VBScript. It is essential to have a good understanding of ‘VBS Date Functions’ for dealing with the smooth working on the concept of ‘Dates’ while writing the code in the scripts. 

This tutorial will explain to you more about the different Date Functions in VBScript with simple examples for your easy and clear understanding.

Date Functions VBscript

Date Functions

In normal scenarios, the Date function is used to display the current system date while working on the script and is the most basic and widely used function which is used while working with the Dates. There are various Date format functions available for converting the Date into different formats.

In some of my earlier tutorials of this series, Dates are used in one or more examples. Date function like cDate is also explained in one of my earlier tutorials.

Let’s take a simple example to understand the usage of this basic Date Function as given below.

Example:

<html>
<head>
<title>Let’s see implementation of a Date Function </title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim val
val = Date
Msgbox “Today’s Date is:” & val
</script>
</body>
</html>

The output is: Today’s Date is: 10/24/2017

Date Function

Let’s take a look at the various types of Date Functions supported by VBScript.

Different Date Functions used in the VBScript

There are multiple Date Functions that are used to perform operations on Dates and some Format functions which help the coders to convert dates from one format to another.

Following is the list of various Date Functions:

#1) cDate:

cDate is used to convert a valid expression into a Date type value. The syntax of this is cDate(date) i.e. Any valid Date/Time expression will be converted into a particular Date.

#2) IsDate:

IsDate is used to check whether a specified expression is of Date type or not. This returns a Boolean value as True in case it is a Date else False. The syntax of this is IsDate(any expression).

#3) Day:

This Date function is used to fetch the day of the month of the date that is specified as a parameter i.e. any value in-between 1-31 as there are 30-31 days in a month. The syntax of this is Day(Date).

#4) Month:

This is used to fetch the month of the year. This returns a number in-between 1-12 for the specified date which is passed as a parameter. The syntax of this is Month(Date).

#5) Year:

This function is used to fetch the year of the specified Date. The syntax of this is Year(Date).

#6) Now:

This function is used very frequently and works like the Date Function that is discussed above. This returns both the current system date as well as time. The syntax of this is Now. By writing simply Now, the current system date and time will be displayed

#7) DateAdd:

This function is used to fetch a Date value after the addition of some specified time interval which is specified as a parameter. The syntax of this is DateAdd(Interval, Number, Date). 

Here, the interval can be any of the following values:

  • d: This is passed if you want to pass days as an interval and then based on the ‘Number’ which is passed, days are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • m: This is passed if you want to pass a month as an interval and then based on the ‘Number’ which is passed, months are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • y: This is passed if you want to pass the day of the year as an interval and then based on the ‘Number’ which is passed, days are either added or subtracted from the Date that is passed as the last parameter in the above function. This is the same as d.
  • yyyy: This is passed if you want to pass the year as an interval and then based on the ‘Number’ which is passed, years are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • q: This is passed if you want to pass the quarter as an interval and then based on the ‘Number’ which is passed, a quarter is either added or subtracted from the Date that is passed as the last parameter in the above function.
  • w: This is passed if you want to pass the weekday as an interval and then based on the ‘Number’ which is passed, weekdays are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • ww: This is passed if you want to pass the week of the year as an interval and then based on the ‘Number’ which is passed, weeks are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • h: This is passed if you want to pass the hour as an interval and then based on the ‘Number’ which is passed, hours are either added or subtracted from the Date that is passed as the last parameter in the above function
  • m: This is passed if you want to pass the minute as an interval and then based on the ‘Number’ which is passed, minutes are either added or subtracted from the Date that is passed as the last parameter in the above function.
  • s: This is passed if you want to pass the second as an interval and then based on the ‘Number’ which is passed, seconds are either added or subtracted from the Date that is passed as the last parameter in the above function.

#8) DateDiff:

This function is used to fetch the difference between the 2 dates that are specified as parameters based on the interval specified. The syntax of this is DateDiff(Interval,Date1,Date2). The value of the interval is the same as discussed above in the DateAdd function.

#9) DatePart:

This is used to fetch some specific part of the date which is specified as a parameter. The syntax of this is DatePart(Interval, Date). The value of the interval is the same as discussed above in the DateAdd function.

#10) MonthName:

This is used to fetch the name of the specified Month which is passed as a parameter inside the brackets. The syntax of this is MonthName(Month value).

#11) FormatDateTime:

This is a format function that is used to convert the Date to some specific format based on the parameters that are supplied to the function. The syntax of this is FormatDateTime(Date,Format). This is a widely used format function

The Format is an optional parameter but as this is widely used, it is good to know about the different format parameter values.

Various Format Parameter values are as follows:

0: If a format value is passed as 0 then it will return the Date in mm/dd/yyyy format along with the time if it is specified in the Date parameter. This is the default value.

1: If a format value is passed as 1 then it will return the Date in Weekday, Month Name, Year format.

2: If a format value is passed as 2 then it will return the Date in mm/dd/yyyy format.

3: If a format value is passed as 3 then it will return the Date in hh:mm: ss PM/AM format if time is specified in the Date parameter.

4: If a format value is passed as 4 then it will return the Date in hh: mm format if time is specified in the Date parameter.

Note: WeekDay and WeekDayName functions are not directly related to Date functions and are already covered in some of the earlier tutorials so I am not covering them again.

Now, let’s understand the usage of these functions with the help of an Example.

Example:

<html>
<head>
<title>Let’s see implementation of various Date Functions </title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim val,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,val11
val = “October 25 , 2017”
valnew = 10/25/2018
val1=CDate(val)
val2=IsDate(val)
val3=Day(val1)
val4=Month(val1)
val5=Year(val1)
val6=Now
val7=DateAdd(“d”,2,val1)
val8=DateDiff(“yyyy”,valnew,val1)
val9=DatePart(“m”,val1)
val10=MonthName(val4)
val11=FormatDateTime(val,2)
Msgbox “Converted Date Value is:” & val1 & “<br />”
Msgbox “Checking if it is Date Value:” & val2 & “<br />”
Msgbox “Day fetched from Date is:” & val3 & “<br />”
Msgbox “Month fetched from Date is:” & val4 & “<br />”
Msgbox “Year fetched from Date is:” & val5 & “<br />”
Msgbox “Current Date Value is:” & val6 & “<br />”
Msgbox “Date Value after addition is:” & val7 & “<br />”
Msgbox “Date Value Difference is:” & val8 & “<br />”
Msgbox “Part fetched from Date Value is:” & val9 & “<br />”
Msgbox “Month Name fetched from Date is:” & val10 & “<br />”
Msgbox “The new format of Date is:” & val11
</script>
</body>
</html>

Output is:

Converted Date Value is:10/25/2017
Checking if it is Date Value: True
Day fetched from Date is:25
Month fetched from Date is:10
Year fetched from Date is:2017
Current Date Value is:10/25/2017 1:48:29 AM
Date Value after addition is: 10/27/2017
Date Value Difference is:1
Part fetched from Date Value is:10
Month Name fetched from Date is:October
The new format of Date is:10/25/2017

Conversion

Conclusion

I am sure that this tutorial would have enabled each of you to gain good knowledge about the Date Functions that are used in the VBScript and this, in turn, will help you in proceeding with the next tutorials of the VBScript series.

Next VBScript Tutorial #9: I will discuss ‘Strings and Cookies’ in the next VBScript tutorial.

Let us know if you have any queries about this tutorial in the comments section below.

Was this helpful?

Thanks for your feedback!

Leave a Comment