Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Made some assumptions and had to write it in C#, but this version of the code gives me 2 months for August 31 and 3 months for August 9</p> <pre><code> public static string ToDuration(DateTime dt, string suffixAgo) { string output; DateTime theDate; if (dt == null) { output = "now"; } else { theDate = dt; TimeSpan DateInterval = DateTime.Now - theDate; int SecondsAppart = Convert.ToInt32(Math.Floor(DateInterval.TotalSeconds)); if ((SecondsAppart &lt; 86400)) { if (SecondsAppart &lt; 59) output = ("less than a minute " + suffixAgo); else if (SecondsAppart &lt; 119) output = ("about a minute " + suffixAgo); else if (SecondsAppart &lt; 3599) output = string.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo); else if (SecondsAppart &lt; 7199) output = "about an hour " + suffixAgo; else output = string.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo); } else { int DaysAppart = Convert.ToInt32(DateInterval.TotalDays); if (DaysAppart &lt;= 1) output = "yesterday"; else if (DaysAppart &lt; 30) output = string.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo); else if (DaysAppart &lt; 60) output = "about a month " + suffixAgo; else if (DaysAppart &lt; 365) output = string.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays/30), suffixAgo); else if (DaysAppart &lt; 730) output = ("about a year " + suffixAgo); else output = string.Format("{0} year {1}", Math.Floor(DateInterval.TotalDays/365), suffixAgo); } } return output; } </code></pre> <p>I've updated the code and I think you have the results you're expecting now. Hopefuly this will help.</p> <p>Cheers, Wagner.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload