A lot of applications require a utility to calculate difference between two dates. the main example is to identify the added date of a person registration, when the person is registered with us and the current date. There are few examples listed below. Example show a customize date of a video that is uploaded on the web.
Added just a minute ago.
Added two minutes ago.
Added three hours ago.
Added four days ago.
Added three weeks ago.
Added two months ago.
Added one year ago.
So i will show you how you can do using ASP.NET. Below is sample code that will provide you a clear understanding about it.
Public Function Return_CustomizeDate(ByVal startdate As DateTime, ByVal enddate As DateTime) As StringDim time As String = ""
Dim diffdate As TimeSpan = enddate.Subtract(startdate)
Dim days As Integer = diffdate.Days
If days >= 365 Then
Dim yr As Double = days / 365
Dim years = CInt(Math.Ceiling(yr))
time = years & " years ago"
ElseIf days >= 31 And days < 365 Then
Dim mn As Double = days / 31
Dim months As Integer = CInt(Math.Ceiling(mn))
time = months & " months ago"
ElseIf days >= 7 And days < 31 Then
Dim wk As Double = days / 7
Dim week As Integer = CInt(Math.Ceiling(wk))
time = week & " weeks ago"
ElseIf days < 7 And days > 0 Then
time = days & " days ago"
ElseIf days = 0 Then
Dim hours As Integer = diffdate.Hours
If hours = 0 Then
Dim minutes = diffdate.Minutes
time = minutes & " minutes ago"
Else
time = hours & " hours ago"
End If
End If
Return time
End Function
So this function takes two dates arguments, first starting date, and ending date and returns customizable date difference.
If you have any type of question, please send us email on irfan.nettech@gmail.com.