VBScript Date Functions and VBScript Time Functions – An Excellent Guide for VBScript Tutorial 6 & 7

vbscript date functions and vbscript time functions

VBScript Tutorial – Table of Content

VBScript Tutorial #1: Overview of VBScript Variables 

VBScript Tutorial #2: VBScript Conditional Statements and Loops

VBScript Tutorial #3: VBScript Procedures

VBScript Tutorial #4: VBScript Error Handling and Execute VBScript

VBScript Tutorial #5: VBScript String Functions

VBScript Tutorial #6: VBScript Date Functions

VBScript Tutorial #7: VBScript Time Functions

VBScript Tutorial #8: VBScript Array Functions

In this VBScript Tutorial, we are going to learn about the most important and frequently used VBScript Date Functions and VBScript Time Functions, including vbscript Cdate, vbscript DateAdd, vbscript date, vbscript time, vbscript FormatDateTime function, etc. All the vbscript date functions and vbscript time functions are explained with examples.

VBScript Tutorial #6: VBScript Date Functions

While working with dates in vbscript, we can use in-build vbscript date functions to perform important date-related operations such as capture system date, date conversion, extract different parts of a date, calculation, formatting, etc. This article(VBScript Date Functions) contains all the important built-in VBScript date functions, which are mostly used in programs. 

VBScript Date Functions – Summary: 

VBScript Date Functions – Details:

All the important vbscript date functions are explained in this section with a proper example.

vbscript Cdate:

vbscript cdate function used to convert a string expression of a date into date format and returns the converted date value. While updating any date type field such as database field with the date data type, we need to convert the string into date format. Otherwise, an error will be thrown.

Syntax: Cdate(date)

Parameter Description:

date – It denotes any valid date and time expression in string format. 

Example:

In the below example, any valid date expression will be converted into date format using vbscript Cdate function.

strDate = "10-Feb-2021"
dtDate = Cdate(strDate)
‘After the date conversion the variable dtDate will hold the value with date format (#10-Feb-2021#).

vbscript Date:

The vbscript date function returns the present system date.

Syntax: Date

Example:

In the below example, we will store the current system date in a variable and display in the message box.

sysDate = Date
msgbox " Current system date is " & sysDate
' OutPut (Msgbox):
' Current system date is 04-Jan-2020

vbscript DateAdd:

vbscript dateadd function returns the calculated date value after adding with specific interval time.

Syntax: DateAdd(interval,number,date)

Parameter Description:

number – It represents any number we want to add. It can be positive(future date) or negative(past date) value.

date – It represents any valid date.

interval – It’s a mandatory parameter which denotes the interval of time. The different interval options are –

· yyyy – Represents the quarter interval.

· q – Represents the quarter interval.

· m – Represents the month interval.

· y – Represents the the day of a year interval.

· d – Represents the day interval.

· w – Represents the weekday interval.

· ww – Represents the week of the year interval.

· h – Represents the hour.

· n – Represents the minute.

· s – Represents the second.

Example:

In the below example, we will calculate the future date after adding two months with the system date using vbscript dateadd function.

sDate = "04-Jan-2021"
newDate = Cdate(sDate)
newDate = DateAdd("m",+2,sDate)
msgbox "Future date after adding 2 months with " & sDate & " is  " & newDate
vbscript date functions - vbscript DateAdd
vbscript date functions – vbscript DateAdd

vbscript DateDiff:

vbscript datediff function returns the number of an interval between two dates.

Syntax: DateDiff(date1,date2[,firstdayofweek[,firstweekofyear]])

Parameter Description:

date1 – It represents any valid date expression.

date2 – It represents any valid date expression.

firstdayofweek – It’s an optional field which denotes the day of the week. The available values are – 

  • 0 = vbUseSystemDayOfWeek (API Setting for National Language Support)
  • 1 = vbSunday (Sunday – default)
  • 2 = vbMonday (Monday)
  • 3 = vbTuesday (Tuesday)
  • 4 = vbWednesday (Wednesday)
  • 5 = vbThursday (Thursday)
  • 6 = vbFriday (Friday)
  • 7 = vbSaturday (Saturday)

firstweekofyear – It’s also an optional field which denotes the first week of the year. The available values are – 

  • 0 = vbUseSystem (API Setting for National Language Support)
  • 1 = vbFirstJan1 (Start with the week when January 1 occurs – default)
  • 2 = vbFirstFourDays (It represent the start week where minimum 4 days are fall in the new year)
  • 3 = vbFirstFullWeek (It represent the week which completely falls in new year)

Example:

In the below example, we are calculating the difference in day interval between two consecutive dates using vbscript datediff function.

date1="04-Jan-2021 00:00:00"
date2="05-Jan-2021 23:59:00" 
diff = DateDiff("d", date1, date2)
msgbox "The date difference - " & diff

' OutPut (Msgbox):
' The date difference - 1
vbscript date functions - vbscript DateDiff
vbscript date functions – vbscript DateDiff

vbscript DatePart:

vbscript datediff function returns the specific part(day, month or year) of any particular date.

Syntax: DatePart(interval,date2,firstdayofweek[,firstweekofyear]])

Parameter Description:

interval – It’s a mandatory parameter which denotes the interval of time. The different interval options are –

· yyyy – Represents the quarter interval.

· q – Represents the quarter interval.

· m – Represents the month interval.

· y – Represents the the day of a year interval.

· d – Represents the day interval.

· w – Represents the weekday interval.

· ww – Represents the week of the year interval.

· h – Represents the hour.

· n – Represents the minute.

· s – Represents the second.

date – It represents any valid date expression.

firstdayofweek – It’s an optional field which denotes the day of the week. The available values are – 

  • 0 = vbUseSystemDayOfWeek (API Setting for National Language Support)
  • 1 = vbSunday (Sunday – default)
  • 2 = vbMonday (Monday)
  • 3 = vbTuesday (Tuesday)
  • 4 = vbWednesday (Wednesday)
  • 5 = vbThursday (Thursday)
  • 6 = vbFriday (Friday)
  • 7 = vbSaturday (Saturday)

firstweekofyear – It’s also an optional field which denotes the first week of the year. The available values are – 

  • 0 = vbUseSystem (API Setting for National Language Support)
  • 1 = vbFirstJan1 (Start with the week when January 1 occurs – default)
  • 2 = vbFirstFourDays (It represent the start week where minimum 4 days are fall in the new year)
  • 3 = vbFirstFullWeek (It represent the week which completely falls in new year)

Example:

Below example evaluates the month part of a given date using vbscript datepart function.

date=Cdate("04-Jan-2021") 
monthPart = DatePart("d", date)
msgbox "The month part - " & monthPart

' OutPut (Msgbox):
' The month part - 1

vbscript IsDate:

vbscript isdate function returns the boolean value as true or false if any string expressing can be converted into date format. Vbscript isdate function is used to test a date expression.

Syntax: Isdate(date)

Parameter Description:

date – It denotes any date expression to be verified. 

Example:

In the below example, any given test expression is checked for the valid date expression.

date = "04-Jan-2021"
boolResult = IsDate(date)
msgbox "Is Valid date ? Ans: " & monthPart

' OutPut (Msgbox):
' Is Valid date ? Ans: true

vbscript Day:

vbscript day function extracts the number(1-31) that represents the day from a valid date expression.

Syntax: Day(date)

Parameter Description:

date – It’s a valid date expression.

Example:

In the below example, the day part will be extracted from a given date using vbscript day function.

date = Cdate("04-Jan-2021")
num = Day(date)
msgbox "The day part is - " & num

' OutPut (Msgbox):
' The day part is - 4

vbscript Month:

vbscript month function extracts  the number(1-12) that represents the month from a valid date expression.

Syntax: Month(date)

Parameter Description:

date – It’s a valid date expression.

Example:

In the below example, month will be extracted from a given date using vbscript month function.

date = Cdate("04-Jan-2021")
num = Month(date)
msgbox "The month part is - " & num

' OutPut (Msgbox):
' The month part is - 1

vbscript Year:

vbscript year function extracts the four digit number that represents the year from a valid date expression.

Syntax: Year(date)

Parameter Description:

date – It’s a valid date expression.

Example:

In the below example, the year will be extracted from a given date using vbscript year function.

date = Cdate("04-Jan-2021")
num = year(date)
msgbox "The year part is - " & num

' OutPut (Msgbox):
' The year part is - 2021

vbscript MonthName:

vbscript monthname function returns the name of a specific month code(1-12).

Syntax: MonthName(month[,abbreviate])

Parameter Description:

month – It’s representing the code(1-12) for any specific month.

abbreviate – It’s not an mandatory parameter. It represents to check about the name of the month is abbreviated or not. The default value is false.

Example:

month_name = MonthName(12)
msgbox "The month name is - " & month_name

' OutPut (Msgbox):
' The month name is - December

vbscript Weekday:

vbscript weekday function returns the number between 1 and 7 that denotes the day of the particular week.

Syntax: WeekDay(date[,firstdayofweek])

Parameter Description:

date – It’s a valid date expression.

firstdayofweek – It’s an optional field which denotes the start day of the week. The available values are –

  • 0 = vbUseSystemDayOfWeek (API Setting for National Language Support)
  • 1 = vbSunday (Sunday – default)
  • 2 = vbMonday (Monday)
  • 3 = vbTuesday (Tuesday)
  • 4 = vbWednesday (Wednesday)
  • 5 = vbThursday (Thursday)
  • 6 = vbFriday (Friday)
  • 7 = vbSaturday (Saturday)

Example:

In the below example, the day representing the week, will be extracted from a given date using vbscript weekday function.

date = Cdate("06-Jan-2021")
num = Weekday(date,1)
msgbox "The week day is - " & num

' OutPut (Msgbox):
' The week day is - 4

vbscript WeekDayName:

vbscript weekdayname function returns the name of a specific day of a week(1-7).

Syntax: WeekDayName(weekday[,abbreviate[,firstdayofweek]])

Parameter Description:

weekday – It’s representing the day code(1-7) for any week.

abbreviateIt’s not an mandatory parameter. It represents to check about the name of the day is abbreviated or not. The default value is false.

firstdayofweek – It’s an optional field which denotes the start day of the week. The available values are –

  • 0 = vbUseSystemDayOfWeek (API Setting for National Language Support)
  • 1 = vbSunday (Sunday – default)
  • 2 = vbMonday (Monday)
  • 3 = vbTuesday (Tuesday)
  • 4 = vbWednesday (Wednesday)
  • 5 = vbThursday (Thursday)
  • 6 = vbFriday (Friday)
  • 7 = vbSaturday (Saturday)

Example:

day_name = WeekdayName(2)
msgbox "The name of the week day - " & day_name

' OutPut (Msgbox):
' The name of the week day - Monday

VBScript Tutorial #7: VBScript Time Functions

While working with time in vbscript, we can use in-build vbscript time functions to perform important time-related operations such as capture system time, extract different parts of any time, calculation, time formatting, etc. This article(VBScript Time Functions) contains all the important built-in VBScript time functions, which are mostly used in programs. 

Important VBScript Time Functions – Summary: 

VBScript Time Functions – Details:

We will explain all the essential vbscript time functions in this section with a proper example.

vbscript Hour:

vbscript hour function extracts the hour of the day as a number between 0 to 23 from time expression.

Syntax: Hour(time)

Parameter Description:

time – It’s a mandatory parameter that represents a valid time expression.

Example:

In the below example, an hour of the day will be extracted from a valid time expression using vbscript hour function.

numHour = Hour("14:40:35")
msgbox "The hour for the day is - " & numHour

' OutPut (Msgbox):
' The hour for the day is - 14

vbscript Minute:

vbscript minute function extracts the minute of the hour as a number between 0 to 59 from time expression.

Syntax: Minute(time)

Parameter Description:

time – It’s a mandatory parameter that represents a valid time expression.

Example:

In the below example, the minute of the hour will be extracted from a valid time expression using vbscript minute function.

numMin = Minute("14:40:35")
msgbox "The minute for the hour is - " & numMin

' OutPut (Msgbox):
' The minute for the hour is - 40

vbscript Second:

vbscript second function extracts the second of the minute as a number between 0 to 59 from time expression.

Syntax: Second(time)

Parameter Description:

time – It’s a mandatory parameter that represents a valid time expression.

Example:

In the below example, the second of the minute will be extracted from a valid time expression using vbscript second function.

numSec = Second("14:40:35")
msgbox "The second for the minute is - " & numSec

' OutPut (Msgbox):
' The second for the minute is - 35

vbscript Time:

vbscript time function returns the current system time.

Syntax: Time

Example:

In the below example, we will store the current system time in a variable and display in a message box.

sysTime = Time
msgbox " Current system time is " & sysTime
' OutPut (Msgbox):
' Current system time is 14:40:35
vbscript time functions - vbscript Time
vbscript time functions – vbscript Time

vbscript Now:

vbscript now function returns the current system date with timestamp.

Syntax: Now

Example:

In the below example, we will store the current system date and time in a variable and display in a message box.

sysTimeStamp = Now
msgbox "Current system date with time is " & sysTimeStamp
' OutPut (Msgbox):
' Current system date with time is 07-Jan-2021 14:40:35

vbscript Timer:

vbscript timer function returns the count of seconds from 12:00 AM.

Syntax: Timer

Example:

secondCounter = Timer
msgbox "Number of seconds since 12:00 AM  " & secondCounter
' OutPut (Msgbox):
' Number of seconds since 12:00 AM 1067.002

vbscript TimeSerial:

vbscript timeserial method fetch the exact time for a mentioned hour, minute  and second.

Syntax: TimeSerial(hour, minute, second)

Parameter Description:

hour – It’s a mandatory numeric parameter, denotes hours.

minute – It’s a mandatory numeric parameter, denotes minutes.

second – It’s a mandatory numeric parameter, denotes seconds.

Example:

In the below example, vbscript timeserial function returns the time for the given hours, minutes and seconds.

time = TimeSerial(15,2,20)
msgbox "Specified time is " & time
' OutPut (Msgbox):
' Specified time is 03:02:20 PM

vbscript FormatDateTime:

vbscript formatdatetime function formats and returns a valid and time expression.

Syntax: FormatDateTime(dateExp, format)

Parameter Description:

dateExp– This is a mandatory parameter. It represents a valid date-time expression.

format – It’s an optional arameter that specifies the date and time format. The available return date and time formats are –

  • 0 = vbGeneralDate – This is the default format value (date format: mm/dd/yyyy and time if specified: hh:mm:ss PM/AM).
  • 1 = vbLongDate (date: weekday, monthname, year)
  • 2 = vbShortDate (date: mm/dd/yyyy)
  • 3 = vbLongTime (time: hh:mm:ss PM/AM)
  • 4 = vbShortTime (Return time: hh:mm)

Example:

In the below example, vbscript timeserial function returns the time for the given hours, minutes and seconds.

d=CDate("06-Jan-2021 13:45")
msgbox "Specified formated date time is " & FormatDateTime(d,1)

' OutPut (Msgbox):
' Specified formated date time is Wednesday, Jan 06, 2021
vbscript time functions - vbscript FormatDateTime
vbscript time functions – vbscript FormatDateTime

Conclusion:

Through this VBScript Date and Time Functions article, we have learned about the learn about the most important and frequently used VBScript Date Functions and VBScript Time functions, including vbscript Cdate, vbscript DateAdd, vbscript FormatDateTime function, etc. We hope this tutorial has helped a lot to brush up on your basics of VB Scripting. If you want to learn more about VBScript, please click here.

About K Mondal

Hi, I am K. Mondal, I am associated with a leading organization. I am having 12+ years of working experience across domains e.g., application development, automation testing, IT Consultant. I am very much interested in learning different technologies. I am here to fulfill my aspiration and currently contributing as an Author and Website Developer both in LambdaGeeks.
Connect to LinkedIn- https://www.linkedin.com/in/kumaresh-mondal/

Leave a Comment

Your email address will not be published. Required fields are marked *