Results 1 to 6 of 6

Thread: how to convert years into months into days

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    how to convert years into months into days

    i want to convert my years and months into days.instead of display like 1 years 3 months and 4 days.i want to be display like 296.4Days equal to 296days.first it was able to covert like 36 days = 1 months6 days but now all i want to convert and add years months into days like 365 + 180 +8 days.stores that value and take the average. i have my code below.since i still not take the average value all i need is to convert back into days(years and month + days)

    Public Function e(length As Integer ) As String


    dim a As string=0
    dim b As string=0
    dim c As string=0
    dim d As string =0

    if e = 0 then
    Return String.Empty
    end if

    if e >= 366 then

    a=cstr(Math.Floor (e/ 365))
    e =(e Mod 365 )
    e =(a * 365)
    a = cstr(e)

    end if

    if e>31Andalsoe<366 then

    b=cstr (Math.Floor(e/ 30.4375))
    e=e Mod 30.4375
    e= (b * 30)
    b=cstr(e
    end if

    if e< 31 Then
    c =cstr(e)

    end if

    if e= 0 then
    Return String.e

    end if

    Return(a+ "days" +b+ "days"+c+ "days") 'display in day not in years nd months

    End Function


    your help will be much appreciated

    thanks in advance

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: how to convert years into months into days

    Take a look at this:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim dtNow As DateTime = DateTime.Now
            Dim dt As DateTime = DateTimePicker1.Value
    
            Dim timeDifference As Integer = dt.Subtract(dtNow).Days
            MessageBox.Show(String.Format("There are {0} day(s) between {1} and {2}", timeDifference, dtNow.ToShortDateString, dt.ToShortDateString))
        End Sub
    End Class
    Last edited by dday9; Oct 30th, 2012 at 05:01 PM. Reason: Fixed my code
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: how to convert years into months into days

    thanks Frenzied,
    im using ssrs 2008..so in my fields text box i enter years months and days..so in my code i have the one above...in my expression text box i take the average from the call function in my code above.here is my expression =code.e(avg(Fields!x.Value))...

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

    Re: how to convert years into months into days

    In order to convert a number of years and months into days, you must have some reference date to indicate years and months from or to that date. That's because not all months or years are the same length. That's exactly why the TimeSpan structure will only use units up to days to represent a time period. So, the only way that you reasonably do the calculation you want is if you have the date at the beginning or end of that time period. You can then call AddYears on that reference date and AddMonths on the result of that to get a Date representing the other end of the time period. Subtract one from the other to get a TimeSpan and its Days property is the value you want.
    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: how to convert years into months into days

    Here is another method that gets the number of months, as well as the number of days between the two dates:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Create variables based on the 2 datetimepickers
            Dim dt1 As DateTime = DateTime.Now
            Dim dt2 As DateTime = DateTimePicker1.Value
    
            'This returns the number of months between the two dates
            Dim monthCount As Long = DateDiff(DateInterval.Month, dt1, dt2)
            MessageBox.Show(String.Format("There are {0} months between {1} and {2}", monthCount, dt1.ToShortDateString, dt2.ToShortDateString))
    
            'This returns the number of days between the two dates
            Dim timeDifference As Integer = dt2.Subtract(dt1).Days
            MessageBox.Show(String.Format("There are {0} day(s) between {1} and {2}", timeDifference, dt1.ToShortDateString, dt2.ToShortDateString))
    
        End Sub
    End Class
    Here is my original post with the function.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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

    Re: how to convert years into months into days

    i want to convert my years and months into days
    Live as long as I have and years become days without any action whatsoever. Life seems to consist of a constant repetition of December 22nd! (Hey, where's the sad smiley on this forum?)
    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