Results 1 to 14 of 14

Thread: Convert an integer to # of years-months-days

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Convert an integer to # of years-months-days

    I need to convert a whole number to the number of
    years, days and months.
    Example: 4142= 11 years, 4, months and 4 days (give or take a day)

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Convert an integer to # of years-months-days

    You can approximate it. It does not really make sense without tying the period of time to a date.
    Code:
    Dim days As Integer = 4145
    Dim dt As DateTime = New DateTime(0) + TimeSpan.FromDays(days)
    Console.WriteLine("{0} days is approximately {1} years {2} months and {3} days." _
    	, days , dt.Year - 1, dt.Month - 1, dt.Day - 1)
    W o t . S i g

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert an integer to # of years-months-days

    As Milk suggests, you really need a reference date in order to be accurate because the number of days in a month varies. If you had a time period of 35 days total then that might be 1 month and 4 days, 1 month and 5 days, 1 month and 6 days or 1 month and 7 days depending on exactly when the period started and ended.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Convert an integer to # of years-months-days

    hi

    thanks for your reply..i have my code below. i want to display if no month(s) and day(s) i want to display only as 2 years and not 2years, 0months, 0 days..
    and in my code below why i get negative(-ve) values for my years months and days
    Public Function GetCustomDate(ByVal days As Int32) As String

    Dim timeSpan as System.TimeSpan
    'timeSpan = objDate2 - objDate1
    Dim objDate1 As DateTime = DateTime.Now()
    Dim objDate2 As DateTime = objDate1.AddDays(1 * days)

    Return (objDate1.Year - objDate2.Year)& "Years" & (objDate1.Year - objDate2.Year) & " Months = " & (objDate1.Month - objDate2.Month) & " Days = " & (objDate1.Day - objDate2.Day)


    thanks for your help

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert an integer to # of years-months-days

    You would to use a some If statements to determine what parts to include and what parts not to include. Just write down the logic you would use if you were going to do it with pen and paper and then write code to implement that logic. You don't need any programming experience for the first part so you can do that at the very least.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Convert an integer to # of years-months-days

    please help me on my questions..

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert an integer to # of years-months-days

    I already did. Please have a go at doing what I suggested.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Lively Member claws135's Avatar
    Join Date
    Oct 2012
    Posts
    106

    Re: Convert an integer to # of years-months-days

    you also should put in a checker of leap years. for every 4 years add a day
    Some Ponies just want to watch the cereal burn.

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Convert an integer to # of years-months-days

    Quote Originally Posted by claws135 View Post
    you also should put in a checker of leap years. for every 4 years add a day
    Not actually as simple as that. Between 1997 and 2011, 14 years, 2 leap years. Between 2003 and 2009, 6 years, also 2 leap years!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10
    Lively Member claws135's Avatar
    Join Date
    Oct 2012
    Posts
    106

    Re: Convert an integer to # of years-months-days

    Quote Originally Posted by dunfiddlin View Post
    Not actually as simple as that. Between 1997 and 2011, 14 years, 2 leap years. Between 2003 and 2009, 6 years, also 2 leap years!
    true, it was just an afterthought
    Some Ponies just want to watch the cereal burn.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Convert an integer to # of years-months-days

    hi,

    thanks guys for your reply all my days should be 30 and years should be 365, don't care if its leap year.i have made the changes and i get my right result as years months not days. i have my code for days only below.

    if i got 230days it should be 7months 20days but i got 7 months 16days its not give me d the right result in days.but this problem always happens to my days only and not years and months,secondly my days too got negative for some like


    Public Function XX(ByVal days As integer) As String

    Dim objDate2 As DateTime = objDate1.AddDays(-1* days)
    dim result as String=0

    If Not (objDate1.Day - objDate2.Day)=0 Then
    result += (objDate1.Day - objDate2.Day) & " Days "
    'result+
    End If

    Return result.Trim()
    thanks in advance

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert an integer to # of years-months-days

    Code:
    Public Function GetTimeString(totalDays As Integer) As String
        Dim timeParts As New List(Of String)
    
        If totalDays > 365 Then
            timeParts.Add((totalDays \ 365) & " years")
            totalDays = totalDays Mod 365
        End If
    
        If totalDays > 30 Then
            timeParts.Add((totalDays \ 30) & " months")
            totalDays = totalDays Mod 30
        End If
    
        If totalDays > 0 Then
            timeParts.Add(totalDays & " days")
        End If
    
        Return String.Join(", " timeParts)
    End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Convert an integer to # of years-months-days

    i have my code below which works fine...but if i have 395days..out display as 1 year 30days which is correct however i want to display that 30days as 1months, 1 year 1 months or if im correct 2 years 365days..it should be 3 years

    Public Function LikeToBe(length As Integer ) As String

    dim length1 As string=0
    dim length2 As string=0
    dim length3 As string=0
    dim ReturnString As string

    ReturnString=””

    If length = 0 then
    Return String.Empty
    end if

    if length >= 366
    length1=cstr((Math.Floor (length / 365)))
    length =(length Mod 365)

    end if

    If length > 31 AndAlso length < 365 Then
    length2 =cstr((Math.Floor(length / 30 )))
    length =(length Mod 30)

    end if

    if length < 31 Then
    length3 =cstr(length)
    End If

    If cint(length1) >0
    ReturnString= length1+" Years "

    End if

    If cint(length2) >0

    ReturnString= ReturnString + length2+" Months "

    End if

    If cint(length3) >0

    ReturnString= ReturnString + length3 + "days"

    End if


    Return (ReturnString)

    End Function
    thanks in advance

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Convert an integer to # of years-months-days

    If it doesn't give the proper results it doesn't 'work fine'! I see you've chosen to completely ignore the answer you were given in the post above your own so exactly what males you think that anybody will now further waste their time giving you advice that you are clearly unwilling to take?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width