简体   繁体   中英

Calculate days in years and months?

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

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

How can we calculate in c#?

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

How to get accurate year and months.

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.

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

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);

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.

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.

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);

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM