Quantcast
Viewing all articles
Browse latest Browse all 46

Answer by Waleed A.K. for Difference in months between two dates

you can use the following extension:Code

public static class Ext{    #region Public Methods    public static int GetAge(this DateTime @this)    {        var today = DateTime.Today;        return ((((today.Year - @this.Year) * 100) + (today.Month - @this.Month)) * 100 + today.Day - @this.Day) / 10000;    }    public static int DiffMonths(this DateTime @from, DateTime @to)    {        return (((((@to.Year - @from.Year) * 12) + (@to.Month - @from.Month)) * 100 + @to.Day - @from.Day) / 100);    }    public static int DiffYears(this DateTime @from, DateTime @to)    {        return ((((@to.Year - @from.Year) * 100) + (@to.Month - @from.Month)) * 100 + @to.Day - @from.Day) / 10000;    }    #endregion Public Methods}

Implementation !

int Age;int years;int Months;//Replace your own datevar d1 = new DateTime(2000, 10, 22);var d2 = new DateTime(2003, 10, 20);//AgeAge = d1.GetAge();Age = d2.GetAge();//positiveyears = d1.DiffYears(d2);Months = d1.DiffMonths(d2);//negativeyears = d2.DiffYears(d1);Months = d2.DiffMonths(d1);//OrMonths = Ext.DiffMonths(d1, d2);years = Ext.DiffYears(d1, d2); 

Viewing all articles
Browse latest Browse all 46

Trending Articles