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)
Printable View
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)
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)
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.
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
Quote:
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
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.
please help me on my questions..
I already did. Please have a go at doing what I suggested.
you also should put in a checker of leap years. for every 4 years add a day
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
thanks in advanceQuote:
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()
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
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
thanks in advanceQuote:
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
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?