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
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
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))...
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.
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.
Re: how to convert years into months into days
Quote:
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?)