C# Convert Days to Months

Conversion or change

You want to programmatically convert a figure in days to a figure in months. To do this, you must average the number of days in a month. In solving this problem, we use an averaged number from Google.

Example

To begin, you can determine that the average month has 30.44 days in it by querying Google. Then, to convert an integer of days to a double of months, you can divide by this constant. In this example, we demonstrate that 200 days is equal to 6.57 months.

This C# example program converts figures in days to months. It uses the average days in a month.

Program that converts days to months [C#]

using System;

class Program
{
    static void Main()
    {
	int days = 200;
	// Divide by this constant to convert days to months.
	const double daysToMonths = 30.4368499;
	{
	    double months = days / daysToMonths;
	    Console.WriteLine("{0} days = {1:0.00} months", days, months);
	}
    }
}

Output

200 days = 6.57 months

Interesting part. The interesting part about this computation is that it converts days to months in an abstract way: the computation is not influenced by what months in particular were covered. If you have a known period of time in a year, this computation would not be ideal. But if you are just in general converting days to months, this is the best way to do it. Leap years also influence the computation.

Summary

The C# programming language

In this article, we described the usage of a constant double to convert days to months in an abstract, general way in the C# programming language. Though not ideal in all program contexts, this conversion can be correctly used in a general sense.

Cast Examples
.NET