Code subject: find the difference between dates in asp.net
Posted: Fri Apr 08, 2011 9:36 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2272 Location: Earth Has thanked: 39 time Have thanks: 61 time
Following code check the difference between dates and return the number of years as difference between two dates
Code:
Function testYear(ByVal inputDate, ByVal testDate, ByVal strDifference) Here we test using years (yyyy), difference in years . strDifference = DateDiff("yyyy", testDate, inputDate) If strDifference < 18 Then Response.Write("Sorry you must be older than 18 years") Else Response.Write("Welcome ,you can enter this site.") End If End Function
Also you can do it for number of days .
Code:
Function diffDays(ByVal inputDate, ByVal testDate, ByVal strDifference) 'Here we test using years (yyyy) testDate = "11/12/2001" 'here we show difference in days. strDifference = DateDiff("d", testDate, inputDate)
print the difference in page. Response.Write(strDifference &" to date "& testDate) End Function
You can use "h" for hour ,"m" for month ,"d" for day ,"yyyy" for year ,"s" for seconds ,"q" quarter ,"n" for minutes.
You can add a units of time to a specific date like the following
Code:
<% Dim dateToday, dateAfterAdd dateToday = Date.Now Response.Write("current date is "& dateToday &"<br>") add 6 years to a date . dateAfterAdd = DateAdd("yyyy", 6, dateToday) Response.Write("After adding 6 Years<b> "& dateAfterAdd &"</b><br>") %>
You can also get a part of date :
Code:
Dim d_mydate d_mydate=Now Get the month part of my date . Response.Write DatePart("m",d_mydate)
the FormatDateTime function takes two parameters ( date ,index for type of format) 0 -> vbGeneralDate 3/14/2010 4:48:49 AM Date and Time 1-> vbLongDate Friday, April 14, 2010 Date in Long format 2->vbShortDate 2/12/2010 Date in m/d/yyyy 3->vbLongTime 1:13:11 AM Long Time 4->vbShortTime 10:00 Short Date
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )