Saturday 18 May 2013

Date time functions in vb script

It is very important that you know how to work with date and time in VBScript as most of the VBScript programs will have date and time involved in it.

Below is the list of all date and time functions in vb script.
  1. date
  2. dateadd
  3. datediff
  4. datepart
  5. dateserial
  6. datevalue
  7. day
  8. hour
  9. minute
  10. second
  11. month
  12. monthname
  13. time
  14. timeserial
  15. timevalue
  16. weekday
  17. weekdayname
  18. year
We are going to have a look at each of these functions and examples in VBScript.
'To find current system date
msgbox " Current System date is -> " & date
'****************************************************************************

'To find the future or past date
msgbox "Tommorrow's date will be " & dateadd("d",1,date)

First argument is Interval Type and it can be of below types.

yyyy - Year
m - Month
d - Day
h - Hour
n - Minute
s - Second

'****************************************************************************
'To find the difference between 2 dates
msgbox "Day Difference between today and tommorrow is " & datediff("yyyy","9-jan-1986",date)

First argument is Interval Type and it can be of below types.

yyyy - Year
m - Month
d - Day
h - Hour
n - Minute
s - Second
'****************************************************************************
'To find the current day like 1,2,3...28
msgbox "Current day is -> " & day(date)

'****************************************************************************
'To find the current hour
msgbox "current hour is -> " & hour(now)

'****************************************************************************
'To find the current minute
msgbox "current minute is -> " & minute(now)

'****************************************************************************
'To find the current second
msgbox "current second is -> " & second(now)
'****************************************************************************

'To find the current month number like 1,2....11,12
msgbox "current month is -> " & month(now) 
'****************************************************************************

'To find the month name like Jan, Feb.....Dec
msgbox "current month name is -> " & monthname(month(now))
'****************************************************************************

'To find the current system time
msgbox "current time is -> " & time
'****************************************************************************

'To find the weekday number like 1,2....7
msgbox "current weekday is -> " & weekday(now)
'****************************************************************************

'To find the name of week day like sunday, monday...saturday
msgbox "current weekday Name is -> " & weekdayname(weekday(now))
'****************************************************************************

'To find the current year
msgbox "current year is ->" & year(now)
'****************************************************************************

'To find the parts of the given time stamp .equivalent to day, minute,hour, second etc
msgbox "Day part of the current timestamp" & datepart("d",now)
'****************************************************************************

'To convert the string to date
msgbox "String to date -> " & datevalue("09-jan-1986")
'****************************************************************************

'To convert the string to time
msgbox "String to time-> " & TimeValue("7:15:49 PM")
'****************************************************************************

'To create date from numbers 
msgbox dateserial(1986,01,09)
'****************************************************************************

'To create time from numbers
msgbox timeserial(17,01,09)
'****************************************************************************

No comments:

Post a Comment

Total Pageviews