|
-
Nov 25th, 2012, 05:06 PM
#1
Thread Starter
Junior Member
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)
-
Nov 25th, 2012, 06:46 PM
#2
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)
-
Nov 25th, 2012, 10:34 PM
#3
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.
-
Nov 26th, 2012, 09:22 PM
#4
Thread Starter
Junior Member
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
-
Nov 26th, 2012, 10:27 PM
#5
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.
-
Nov 27th, 2012, 12:17 AM
#6
Thread Starter
Junior Member
Re: Convert an integer to # of years-months-days
please help me on my questions..
-
Nov 27th, 2012, 12:27 AM
#7
Re: Convert an integer to # of years-months-days
I already did. Please have a go at doing what I suggested.
-
Nov 27th, 2012, 03:02 PM
#8
Lively Member
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.
-
Nov 27th, 2012, 03:17 PM
#9
Re: Convert an integer to # of years-months-days
 Originally Posted by claws135
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!
-
Nov 27th, 2012, 03:19 PM
#10
Lively Member
Re: Convert an integer to # of years-months-days
 Originally Posted by dunfiddlin
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.
-
Nov 28th, 2012, 05:17 PM
#11
Thread Starter
Junior Member
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
-
Nov 28th, 2012, 07:34 PM
#12
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
-
Dec 5th, 2012, 03:51 PM
#13
Thread Starter
Junior Member
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
-
Dec 5th, 2012, 04:08 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|