public partial class Form1 : Form{ public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label3.Text = new DateDifference(Convert.ToDateTime("2018-09-13"), Convert.ToDateTime("2018-11-15")).ToString(); label2.Text = new DateDifference(Convert.ToDateTime("2018-10-12"), Convert.ToDateTime("2018-11-15")).ToString(); DateDifference oDateDifference = new DateDifference(Convert.ToDateTime("2018-11-12")); label1.Text = oDateDifference.ToString(); }}public class DateDifference{ public DateTime start { get; set; } public DateTime currentDAte { get; set; } public DateTime origstart { get; set; } public DateTime origCurrentDAte { get; set; } int days { get; set; } int months { get; set; } int years { get; set; } public DateDifference(DateTime postedDate, DateTime currentDAte) { this.start = this.removeTime(postedDate); this.currentDAte = this.removeTime(currentDAte); this.origstart = postedDate; this.origCurrentDAte = currentDAte; } public DateDifference(DateTime postedDate) { DateTime currentDate_ = DateTime.Now; this.start = this.removeTime(postedDate); this.currentDAte = this.removeTime(currentDate_); this.origstart = postedDate; this.origCurrentDAte = currentDate_; if (start > this.currentDAte) { throw new Exception("Current date is greater than date posted"); } this.compute(); } void compute() { while (this.start.Year <= this.currentDAte.Year) { if (this.start.Year <= this.currentDAte.Year && (this.start.AddMonths(1) <= this.currentDAte)) {++this.months; this.start = this.start.AddMonths(1); } if ((this.start.Year == this.currentDAte.Year) && (this.start >= this.currentDAte.AddMonths(-1) && this.start <= this.currentDAte)) { break; } } while (this.start.DayOfYear < this.currentDAte.DayOfYear) {++this.days; this.start = start.AddDays(1); } if (this.months > 11) { while (this.months > 11) {++this.years; this.months = months - 12; } } } public override string ToString() { if (this.start > this.currentDAte) { throw new Exception("Current date is greater than date posted"); } String ret = this.ComposeTostring(); this.reset(); return ret; } private String ComposeTostring() { this.compute(); if (this.years > 0) { if (this.months > 0) { if (this.days > 0) { return String.Format("{0} year{1}, {2} month{3} && {4} Day{5} ago", this.years, plural(this.years), this.months, plural(this.months), this.days, plural(this.days)); } return String.Format("{0} year{1}, {2} month{3} ago", this.years, plural(this.years), this.months, plural(this.months)); } else { if (this.days > 0) { return String.Format("{0} year{1},{2} day{3} ago", this.years, plural(this.years), this.days, plural(this.days)); } return String.Format("{0} year{1} ago", this.years, plural(this.years)); } } if (this.months > 0) { if (this.days > 0) { return String.Format("{0} month{1}, {2} day{3} ago", this.months, plural(this.months), this.days, plural(this.days)); } else { return String.Format("{0} month{1} ago", this.months, plural(this.months)); } } if ((this.origCurrentDAte - this.origstart).Days > 0) { int daysDiff = (this.origCurrentDAte - this.origstart).Days; this.origstart = this.origstart.AddDays(daysDiff); int HoursDiff = (this.origCurrentDAte - this.origstart).Hours; return String.Format("{0} day{1}, {2} hour{3} ago", daysDiff, plural(daysDiff), HoursDiff, plural(HoursDiff)); } else if ((this.origCurrentDAte - this.origstart).Hours > 0) { int HoursDiff = (this.origCurrentDAte - this.origstart).Hours; this.origstart = this.origstart.AddHours(HoursDiff); int MinDiff = (this.origCurrentDAte - this.origstart).Minutes; return String.Format("{0} hour{1}, {2} minute{3} ago", HoursDiff, plural(HoursDiff), MinDiff, plural(MinDiff)); } else if ((this.origCurrentDAte - this.origstart).Minutes > 0) { int MinDiff = (this.origCurrentDAte - this.origstart).Minutes; this.origstart = this.origstart.AddMinutes(MinDiff); int SecDiff = (this.origCurrentDAte - this.origstart).Seconds; return String.Format("{0} minute{1}, {2} second{3} ago", MinDiff, plural(MinDiff), SecDiff, plural(SecDiff)); } else if ((this.origCurrentDAte - this.origstart).Seconds > 0) { int sec = (this.origCurrentDAte - this.origstart).Seconds; return String.Format("{0} second{1}", sec, plural(sec)); } return ""; } String plural(int val) { return (val > 1 ? "s" : String.Empty); } DateTime removeTime(DateTime dtime) { dtime = dtime.AddHours(-dtime.Hour); dtime = dtime.AddMinutes(-dtime.Minute); dtime = dtime.AddSeconds(-dtime.Second); return dtime; } public void reset() { this.days = 0; this.months = 0; this.years = 0; this.start = DateTime.MinValue; this.currentDAte = DateTime.MinValue; this.origstart = DateTime.MinValue; this.origCurrentDAte = DateTime.MinValue; }}
↧
Answer by Patrick Oniel Bernardo for Difference in months between two dates
↧