Avatar of pvg1975
pvg1975
Flag for Argentina asked on

Date difference. Date in format 20110529

Hello all!

I have a field on a database that contains an integer number. Its supposed to be a date. The format is yyyyMMdd, for example: 20110529

Is it possible to know the time difference (in months) in between the field value, and the current date?

I use asp.net (vb)

Thanks!
ASP.NET

Avatar of undefined
Last Comment
pvg1975

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
raysonlee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pvg1975

ASKER
It didnt work, maybe cause my date is an integer value and not a date value?
SOLUTION
disrupt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
raysonlee

if yourdate is not "DATE", you can change it by:
 Dim myDate As Date = "#" + yourdate.tostring.Substring(0, 4) + "-" + yourdate.tostring.Substring(4, 2) + "-" + yourdate.tostring.Substring(6, 2) + "#"
Dim NumMonth as Integer = DateDiff(DateInterval.Month, myDate, Today())
disrupt

source: http://forums.asp.net/t/1163565.aspx/2/10?calculate+number+of+months+between+two+dates+vb+net

Then use something like this:
Public Function monthDifference(startDate As DateTime, endDate As DateTime) As Integer

	Dim systemStartDate As New DateTime()
	Dim timeDifference As TimeSpan


	If endDate > startDate Then
		timeDifference = endDate.Subtract(startDate)
	Else
		timeDifference = startDate.Subtract(endDate)
	End If


	Dim generatedDate As DateTime = systemStartDate.Add(timeDifference)
	Dim noOfYears As Integer = generatedDate.Year - 1
	Dim noOfMonths As Integer = generatedDate.Month - 1

	noOfMonths = noOfMonths + (noOfYears * 12)

	Return noOfMonths

End Function

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
disrupt

scratch that use this tested and works:
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim d1 As DateTime = DateTime.Now
        Dim number As Integer = 20110529

        Dim d2 As DateTime = Number2Date(number)

        Dim M As Integer = Math.Abs((d1.Year - d2.Year))
        Dim months As Integer = ((M * 12) + Math.Abs((d1.Month - d2.Month)))

        Response.Write(months)

    End Sub

    Public Function Number2Date(ByVal Number As Long) As Date
        Dim S As String = Number
        Dim D, M, Y As Integer
        M = S.Substring(4, 2)
        D = S.Substring(6, 2)
        Y = S.Substring(0, 4)
        Return New DateTime(Y, M, D)
    End Function

    Public Function monthDifference(startDate As DateTime, endDate As DateTime) As Integer

        Dim systemStartDate As New DateTime()
        Dim timeDifference As TimeSpan


        If endDate > startDate Then
            timeDifference = endDate.Subtract(startDate)
        Else
            timeDifference = startDate.Subtract(endDate)
        End If


        Dim generatedDate As DateTime = systemStartDate.Add(timeDifference)
        Dim noOfYears As Integer = generatedDate.Year - 1
        Dim noOfMonths As Integer = generatedDate.Month - 1

        noOfMonths = noOfMonths + (noOfYears * 12)

        Return noOfMonths

    End Function
End Class

Open in new window

pvg1975

ASKER
Thank you all for your answers. I ended up doing this think work as follows:

            Dim DatabaseValue As Integer = Expira
            Dim dt As DateTime
            If DateTime.TryParseExact(DatabaseValue, "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
                Dim strDT As Date = dt.ToString("M/d/yyyy")
                Label2.Text = DateDiff(DateInterval.Month, strDT, Now.Today)
            End If
disrupt

edited (don't need last function)
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim d1 As DateTime = DateTime.Now
        Dim number As Integer = 20110529

        Dim d2 As DateTime = Number2Date(number)

        Dim M As Integer = Math.Abs((d1.Year - d2.Year))
        Dim months As Integer = ((M * 12) + Math.Abs((d1.Month - d2.Month)))

        Response.Write(months)

    End Sub

    Public Function Number2Date(ByVal Number As Long) As Date
        Dim S As String = Number
        Dim D, M, Y As Integer
        M = S.Substring(4, 2)
        D = S.Substring(6, 2)
        Y = S.Substring(0, 4)
        Return New DateTime(Y, M, D)
    End Function

End Class

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pvg1975

ASKER
           Dim DatabaseValue As Integer = 20110315
            Dim dt As DateTime
            If DateTime.TryParseExact(DatabaseValue, "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
                Dim strDT As Date = dt.ToString("M/d/yyyy")
                Label2.Text = DateDiff(DateInterval.Month, strDT, Now.Today)
            End If