Working with date and time in ASP VBScript

Here is a sweet way to perform a function based on the date.

If DateDiff(“n”,Now(),”9/15/13 08:00:00″) <= 0 Then
openorclosed = “open”  ‘open form to public access.
Else
openorclosed = “closed”  ‘close form to public access.
End If

 

Lets break down the use of the DateDiff function. From the msdn docs I see DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Interval lets us decide how precise we want this function to be. If we want, we can call it as broad as a  year or down to the second we want to define.

For example, since its December and all the kids are freaking out, lets make a Christmas countdown timer to show how many days until Christmas.

today = Now()
christmas = “12/25/2013”
interval = “d” ‘ we use d since we want to count the days.
numberofdays = DateDiff(interval, today, christmas)
Response.Write(“<p>There are only ” &  numberofdays & ” days until Christmas.<p>”)

Screen shot of code display
Here is the output of the script.

 

To really automate the open and closing of a form, I need to see if the date is in the range of open and closed dates provided by administration.

opendate = “July 1, 2015″
closedate=”April 20, 2015”

If CDate(NOW()) > opendate and CDate(NOW()) < closedate Then
response.write(“<p>the form should be open</p>”)
Else
response.write(“<p>the form should be closed</p>”)
End If

 

How about if I want to format a date to look good for the user. In some cases we would want the date to look formal: Lets take Christmas:

 

christmas_date=”12/25/2015 24:00:00″

FormatDateTime(open_date, 1) // this would output “Wednesday, December 25th, 2015”

FormatDateTime(open_date, 3) // This would output “12:00 AM”

 

Response.write(date & “<hr>”)
Response.Write(MonthName(Month(Date)) & “<hr />”)
Response.Write(Day(Date) & “<hr>”)

Response.Write(Month(“8/10/2008”))

Response.Write(Month(“Aug 10, 2008”))

Response.Write(Month(“10 Aug, 2008”))

Response.Write(Month(“10 August, 2008”))

Response.Write(Month(“August 10, 2008”))

Response.Write(“<br />”)
scrn1

 

Leave a Reply