Free Guides

Language Tutorials


           

ASP ( Active Server Pages )

                 Index              

Your Ad Here

 

Finding Today date and time and changing the format on location

In ASP using VBScript let us learn how to get present date, time in different formats. As we run ASP as a server side scripting process using VBScript as language, the time or date we will get is the time or date of the server. NOT THE LOCAL TIME OF THE CUMPUTER we are using to access the page. Local time can be found out by using client side JavaScript function for date and time. In ASP we will start with printing present ( today ) date. Here is the format.

d_today=Date


Here d_today is the variable and it will store the value of Date. Here is the code to display the present date.
 

Dim d_today
d_today=Date
Response.Write d_today
 

To display the time along with the date we have to use Now.
 

Dim d_today
d_today=Now
Response.Write d_today

The code above will print todays date along with time Hour: Minute : Second.
To display only time we can use Time function.
 

D_today=Time



We can change the date format displayed based on our requirement by using SetLocal function. Let us try to display date in UK English format. Here is the code.

 

Dim d_today,var_Locale
var_Locale = SetLocale(2057) ' UK
d_today=Now ' Along with time
Response.Write d_today
 


 

Display of date and time in local format in ASP using FormatDateTime

We can display Formatted date and time by using FormatDateTime function of VBScript within ASP. This function can take only date , only time or both date and time and display in specified format. Here is the format the function FormatDateTime uses.
 

FormatDateTime(my_date,Format)

Here my_date is the date variable and Format takes the value of 0 to 4 and format the display based on this. The table at the end of this page displays all possible formats with different value of Format.
 

Here is the code to use FromatDateTime function.
 

Dim my_date,var_Locale
var_Locale = SetLocale(1033)
my_date=Now
Response.Write FormatDateTime(my_date,0)
 

Here the Format value is specified as 0 so the output of this is here .
 

7/19/2006 8:38:39 PM

       For different values of Format ( 0 to 4 ) Here is a table of displays. Note that SetLocale is set to USA format. For other list of LCID value of local formats please see the article on today date and time.                      

Format Display Type Details
0 vbGeneralDate 7/19/2006 8:38:39 PM Date and Time
1 vbLongDate Wednesday, July 19, 2006 Date in Long format
2 vbShortDate 7/19/2006 Date in m/d/yyyy
3 vbLongTime 9:53:22 PM Long Time
4 vbShortTime 15:53 Short Date

DateAdd function to add date and time

We can add date by using DateAdd function of VBScript using in ASP. We can add any type of value like day, month , year etc using DateAdd function. Here is the syntax for the DateAdd function.

DateAdd ( date_type, increment_by, date)

Here date_type is the type of date to be added. It can be month, year, Day, Hour, minute etc. Different values are available in a table at the end of this tutorial. The argument increment_by is the interval we want to add to the third argument date.

Here is the code explaining the use of DateAdd function.

Dim d_today,d_added
d_today=Date
Response.Write "Today date is " & d_today & "<br>"
d_added=DateAdd("yyyy",5,d_today)
Response.Write "Add 5 Years " & d_added & "<br>"

In the above code we have added five years to the current date. By changing the value of date_type we can add month, days etc to the present date. Here is the code to add 6 months to today.
 

d_added=DateAdd("m",6,d_today)

DateAdd function will not return any invalid date. Let us try with this example.

d_added=DateAdd("d",32,#1/28/04#)

This line will return 2/29/2004,

Now let us change the year to 2003 and see the result

d_added=DateAdd("d",32,#1/28/03#)

The output of above line is 3/1/2003
This way we can add different types of dates to an existing date & time.
Table for different date_type values
 

Value Meaning
"yyyy" Year
"m" Month
"d" Day
"h" Hour
"n" Minute
"s" Second
"w" Weekday
"ww" Week of the year
"q" Quarter

Finding out difference in two dates by using DateDiff function

We can find out the difference between two dates by using DateDiff function of VBScript used in ASP. DateDiff() finds out the difference of the dates based on the required date part like month, year, days etc. Here is the basic syntax of DateDiff function.
 

DateDiff(date_type, date1, date2, first_dow, first_woy)


Here date_type is the date part to be considered while returning the output. This can be month, day, year etc. The table with different values of date_type is listed at the end. Next two arguments date1 and date2 are two date values for which difference is to be calculated. The other two arguments first_dow and first_woy are optional arguments. First_dow takes the value to specify what is to be taken as first day of the week and the other value first_woy takes the value of first week of the year.

Let us see this example code on uses of DateDiff to find out difference in months between two dates.
 

Dim d_diff
Response.Write "Today date is " & d_today & "<br>"
d_diff=DateDiff("m",#2/28/03#,#2/28/06#)
Response.Write "Difference " & d_diff & "<br>"
 

The output of the above by changing the value of date_type we can find out the difference in days, years, weeks etc. Table for different date_type values

Value Meaning
"yyyy" Year
"m" Month
"d" Day
"h" Hour
"n" Minute
"s" Second
"w" Weekday
"ww" Week of the year
"q" Quarter


 

Collecting parts of Date by using DatePart function

We can collect part of the date like year, month, day, date, Weekday etc from date by using DatePart function in VBScript used in ASP. We can extract the required date portion from the full date or date variable. Here is the syntax of the DatePart function.
 

DatePart(date_type, date)

Here date_type is the type of date part we want to get like Day, month, year, week etc. date is the date variable or a fixed date figure. Here is the code to display month part from today's date.
 

Dim d_today
d_today=Now
Response.Write DatePart("m",d_today)
 

Above code will return month in number for today's date. By changing date_type argument to different values we can get year, day , day of the year etc by using this function.

Let us try to collect year from a fixed date . Here is the example

Response.Write DatePart("yyyy",#09/10/2003#)

We will get an out put of 2003

We can also get the Date, Month or year by using Day(date), Month(date) and Year(date) function respectively. For example
 

year(#10/15/2003#)

This will give 2003 as output.

Month(#10/15/2003#)

This will give 10 as output.
 

Day(#10/15/2003#)

This will give 15 as output.
 

Getting Name of the Month by entering month number

We can get the months name by using the month number by using MonthName() function.
Here is the syntax for MonthName function.
 

MonthName(10)

Output of the above code is October.

It can take one more optional argument to return month name in long or short format. By default it is set to False
 

MonthName(10,False)

This will return October
 

MonthName(10, True )

This will return Oct

Getting Weekday names by using WeekDay and WeekDayName functions in ASP

From a date variable or from a date we can get the weekday number staring from Sunday as 1 and Saturday as 7 ( see the table at the end ) by using weekday() function. Here is the syntax of weekday function.

WeekDay(#7/25/2006#)

Here we have taken one date but we can also use any date variable as input to the WeekDay function. Like this …
 

WeekDay(d_mydate)

Here d_mydate is a date variable and we can assign any date value to it.

The output of this function will be between 1 and 7 depending on the weekday of the input value. The example
 

WeekDay(#7/25/2006#)

The output of above line is 3. It represents Tuesday of the week. To get directly the name of the weekday we can use WeekDayName() function. This function takes the number of weekday and returns the name of the weekday. Here is the syntax of WeekDayName() function

WeekDayName(weekday_number, type)


Here weekday_number is the variable holding the weekday number, type is an optional Boolean argument to specify long or short name of the weekday to be returned. False value of type returns Long name and True value returns short name. By default it is set to False. Here are some examples of WeekDayName function.

 

Response.Write WeekDayName(4)

Response.Write WeekDayName(4,True)

This will give output Wed

Response.Write WeekDayName(5,False)

Table for WeekDays function values

Value Meaning
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday

Generating Date variables by using DateSerial function in ASP

By dateserial function in VBScript used in ASP we can generate date variable by using different variables for day, month and year. This is a quite useful as we can take different user input and create a date variable out of it. We can't create a date variable by placing the variables within two # marks. Like this
 

My_date=#my_month, my_day,1985#

The above code will give error message. Let us start with the syntax of dateserial function.
 

DateSerial(yyyy, dd, mm)

The above function takes three inputs month, date and year and generates a date variable. The date variable then can be used for normal operations. Here is an example of generating date variable.

Dim d1, d2, d3
d1="2007"
d2="2"
d3="28"
Response.Write DateSerial(d1,d2,d3)
 

The above code will return 2/28/2007.

DateSerial can be used to generate different dates before or after the day entered. Here is an example to get next day of the given date.
 

Response.Write DateSerial(d1,d2,d3+1)



Same way we can get the date variable for one month before the dates like this

 

Response.Write DateSerial(d1,d2-1,d3)

For five years advance date, here it is
 

Response.Write DateSerial(d1+5,d2,d3)




DateSerial function takes care of leap years also. Check this code again

Dim d1, d2, d3
d1="2007"
d2="2"
d3="28"
Response.Write DateSerial(d1,d2,d3+1)

 

We will get 3/1/2007 as output but if we change the value of year to 2008 ( d1="2008") then the out put will change to 2/29/2008

DateSerial function is mostly used to collect user enter data and generating a date variable out of it.

Function IsDate to check valid date or date variable

We can check (or validate) the date variable or a date by using IsDate() function. User entered date values in a form are to be checked for correct date or not before further processing. This function takes one input and gives output as TRUE or FALSE based on the input. A valid date or date variable will return TRUE or it will return FALSE. Here is the syntax of IsDate() function.
 

IsDate(my_date)

Here my_date is a date variable or a date itself. Out put of IsDate function is either True or False. Here is the complete code of IsDate() function.

 

my_date="02/28/2007"
Response.Write my_date & " = " & IsDate(my_date)



Here IsDate function will return True. If we change the variable my_date like this.

My_date="02/29/2007"

Then the output of IsDate() function will be false.

IsDate function is useful where user entered date value is to be checked for a valid date for further processing. Whatever date entered by the user through a form can also be checked by using this function.

User entered date value validation in ASP

We can collect date values from user and check the inputs for a valid date or not by using IsDate () function. Here we will give the options to the user to select month, date and year from list boxes and text box and then create a date variable using it. Then we will check the date variable and find out it is a valid date or not.

We will display a message accordingly saying correct date or not.

Here is the code below to check the user entered date value

<%@ Language=VBScript %>
<% Option Explicit %>

<html><head>
<title>(Type a title for your page here)</title>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<%

Dim todo
todo=Request("todo")
if todo="submit" then
Dim month, dt, year, my_date
month=Request("month")
dt=Request("dt")
year=Request("year")
my_date=year + "/" + month + "/" + dt
if(IsDate(my_date)) then
Response.Write month + "/" + dt + "/" + year & " is a valid date "
Else
Response.Write month + "/" + dt + "/" + year & " is a Not a valid date "

End If
End If
%>
<form method=post name=f1 action=''><input type=hidden name=todo value=submit>
<table border="0" cellspacing="0" >
<tr><td align=left >

<select name=month value=''>Select Month</option>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>

</td><td align=left >
Date<select name=dt >
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>

<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
 

</td><td align=left >
Year(yyyy)<input type=text name=year size=4 value=2006>
<input type=submit value=Submit>
</table>
</form>

</body>
</html>