Switch to full style
ASP/ASP.net examples
Post a reply

find the difference between dates in asp.net

Fri Apr 08, 2011 9:36 pm

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)
 


You can also format the date :
Code:

currDate
=Now 
Response
.Write "Date Formatted : "FormatDateTime(currDate,0)
 

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



Post a reply
  Related Posts  to : find the difference between dates in asp.net
 Get Difference in days between two dates as a number     -  
 DYNAMIC DATES IN MYSQL     -  
 Formatting dates in mysql     -  
 Comparing Dates in Java     -  
 Find MAC Address     -  
 Find entity by id     -  
 find a string     -  
 how to find size without using sizeof     -  
 cannot find symbol constructor     -  
 Not to find nativeEncoder class jar     -  

Topic Tags

ASP Time