tags:

views:

195

answers:

10

How to calculate days in years and months in c#? per example :

If 1. days = 385 then I need to display Year= 1.1 (i.e. 1 year 1 month) 2. days= 234 then I need to display year =0.7 (i.e 0 year 7 months)

How can we calculate in c#?

I did for days=234 /365 and result is coming 0.64 (i.e. 0 year 6 months). But actually it is 7 months.

How to get accurate year and months.

A: 

I think that's numerically impossible. What would you do about 1 year and 11 months? 1.11? Because that could mean either 1 year and 1 month and something, or 1 year and 11 months.

Rei Miyasaka
A: 

My suggestion would be to use DateTime.AddDays: it will give you all you need. You can also add other time units there:

    DateTime f = new DateTime(0);
    var y = f.AddDays(361);
Andrey Breslav
+5  A: 

You can do:

double temp  = (234.0 /365.0) * 12.0;

int years = (int)temp;
int months = (int)(temp - years);

This is because you were getting 0.64, which is 0.64 years. If you want months, you'd need to multiply that times 12.

In the above, you'll get 0 years and 7 months... That being said, I'm not sure exactly how you want to format this:

string yearsString = string.Format("{0}.{1}", years, months);

Just be aware that this will do 3.11 for 11 months, which is going to be odd, though it was your requirement.

Also, if you want to have this be very general, you might want to use 365.25 instead of 365 to represent a single Julian Year, as it will help you reduce issues due to leap years.

Reed Copsey
@Reed - this form is sometimes used to express age of kids in schools, and for doctors
Steve Townsend
@Steve: I wasn't arguing with the need for it - just pointing out the potential for confusion. That being said, I showed how to generate it here...
Reed Copsey
@Reed - agreed and +1 for your trouble
Steve Townsend
+2  A: 

If you don't know the actual dates, then you could estimate:

Number of years: x / 365
Number of months: (x % 365) / 30

where % is modulo

Even Mien
You need to be careful here - this could easily generate 2 years and 12 months ;)
Reed Copsey
A: 

Is 60 days = Feb or March ?

Vivek
This should be a comment
SwDevMan81
Maybe it's صفر, or Brumaire, or Solmonað :)
Jon Hanna
My point was... 31 - Jan + 28 - Feb + 1 - March Or was it 31- Jan + 29 - Feb ?
Vivek
My point was that months as a unit doesn't relate to any particular months on any particular calendar.
Jon Hanna
+1  A: 

Assuming a month of exactly one-twelfth of a year, and that you want ignore partial months (based on your saying you expect 7 from your example with 7.688 months, then:

int days = 234;
double years = (double)days / 365.242199;
int wholeYears = (int)Math.Floor(years);
double partYears = years - wholeYears;
double approxMonths = partYears * 12;
string horribleFormat = string.Concat(wholeYears, ".", approxMonths);
Jon Hanna
A: 

Pseudo code

ts= TimeSpan.fromDays(385)
Years =  ts.days Modulo 365
months = (ts.days remainder 365) modulo 12
days = (ts.days remainder 365) Remainder 12
answer string years + "." + months + "." + days
Roadie57
modulo = remainder
Austin Salonen
@Austin Thanks for pointing that out I was thinking Integer division \ in vb ts= TimeSpan.fromDays(385)Years = ts.days \ 365months = (ts.days remainder 365) \ 12days = (ts.days remainder 365) Remainder 12answer string years + "." + months + "." + days
Roadie57
+1  A: 

Are you sure you want this format? The result--at least with the current information provided--will be fuzzy since months are inconsistent lengths.

Consider a couple alternatives:

  • A Year-only representation, such as 1.25 meaning "1 and one quarter years". This doesn't mix months and years, and as such remains simple since a year is 365 days (except for leap years). It also removes ambiguity such as "does 1.10 == 1.1?"

  • Using a concrete start date which would allow you to use strongly-type dates. You could easily use a .ToString() with date-formatting arguments to quickly and accurately get your result.

STW
A: 

Let do some calculations.

1 mon = 0.1
2 mon = 0.2
.
.
9 mon = 0.9
10 mon = 1.0 [WRONG according to you 1 is a year]
fine then 1 / 12 = 0.083, therefore 0.083 is 1 month

Now, 
234 / 365 = 0.64 => 0.64 / 0.083 => 7.7  i.e. 7th month

Therefore fx => days / 365 = ans  % 0.083 = result.

I have no time to prove other number but you can try around this formula.

Ibrahim
A: 

In VB

    Dim d As DateTime = DateTime.MinValue 'year = 0001
    d = d.AddYears(DateTime.Now.Year - 1) 'add current year
    d = d.AddDays(234) 'add days

    Dim yrs As Integer = d.Year - DateTime.Now.Year 'calculate years
    Dim mos As Double = d.Month / 12 'calculate months
    Dim answer As Double = yrs + mos

answer = .6666666666

dbasnett