将天数转换为年,月和日 [英] Convert number of days into year, months and days
问题描述
我下面有我的代码.我已经能够转换成年月日,所以我将它们加在一起以找到平均值.所以如果我有例如average = 396,我的结果应该是1年,1个月和6天dis只是一个例子..我希望我的输出像这样.
i have my code below.i have been able to convert into years months and days.so i add them together to find the average.so if i have for example average = 396 my result should be 1 year, 1month and 6days dis is just and example..i want my output to be like this.
Public Function NaturalLength(length As integer) As Double
'dim d as Date
dim year As double
dim month As double
dim day As double
dim sum as double
'if length =0
'Return String.Empty()
'end if
if length >365.25 then
year =length/ 365.25
length =length Mod 365.25
length =year *365.25
year =cstr(Math.Floor (length/ 365.25))
year=CInt(fix(length))
'length =(length Mod 365.25 )
year = length
end if
if length>=31 And length<366 then
month=length/30.4375
length=length Mod 30.4375
length=month * 30.4375
month=cstr(Math.Floor (length/30.4375))
month=CInt(fix(length))
'length =(length Mod 30.4375)
month=length
end if
if length<31 Then
day =length
end if
sum =(year + month +day)
return sum.tostring()
'return sum.tostring(year+"year" +month+"month"+day+"day")
End Function
在我的表达式文本框中,我有
in my expression text box i have
=code.NaturalLength(Fields!XXX.value)
我需要帮助,但尝试其他计算时遇到错误..我的平均值现在还可以,但是要转换成数月和数日,我仍然无法理解.
在此先感谢
i need help on this i try different calculation i got an error..my average looks ok now but to convert in years months and days i still not get into it.
thanks in advance
推荐答案
如果您真的不满意TimeSpan [ ^ ]对象,那么您可能想要这样的东西:
If you really aren''t happy with the TimeSpan[^] object, then you probably want something like:
Public Sub NaturalLength(length As Integer, ByRef years As Integer, ByRef months As Integer, ByRef days As Integer)
Dim remain As Double
Dim amount As Double
remain = CType(length, Double)
amount = remain / 365.25
years = Math.Truncate(amount)
remain = remain - years * 365.25
amount = remain / 30.4375
months = Math.Truncate(amount)
remain = remain - months * 30.4375
days = Math.Truncate(remain)
End Sub
Sub Main() ' Just a test
Dim length As Integer = 396
Dim y, m, d As Integer
NaturalLength(length, y, m, d)
Console.WriteLine("Years: {0}, months: {0}, days = {0}", y, m, d)
End Sub
顺便说一句:
By the way:
例如,平均值= 396,我的结果应该是1年,1个月和6天
for example average = 396 my result should be 1 year, 1month and 6days
对我来说似乎是错误的.
looks wrong to me.
sum =(year + month +day)
return sum.tostring()
你在这里做什么?只是将所有这些值加到double中并返回该值?看起来它可能无法如此处所述返回您想要的内容:
What are you doing here? Just adding all these values into a double and returning that? It does not look like it can possibly return what you want as described here:
因此,例如,如果我的平均值= 396,则我的结果应该是1年,1个月和6天,而dis就是例子.希望我的输出像这样.
so if i have for example average = 396 my result should be 1 year, 1month and 6days dis is just and example..i want my output to be like this.
看起来您即将获得所需的东西,只需考虑要对计算出的年,月和日值进行什么操作即可.
Looks like you are close to getting what you want, just think about what you want to do with the year, month, and day values you have calculated.
这篇关于将天数转换为年,月和日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!