I need help with the last part of my time difference script it displays 0 min, 0 seconds I’m hoping someone can resolve this for me please.
mu is MembershupUser
PrintTimeSpan1(DateTime.Now - mu.LastActivityDate);
public string PrintTimeSpan1(TimeSpan p)
{
var sb = new StringBuilder();
if (p.Days > 365)
sb.AppendFormat("{0} yr, ", (int)Math.Floor(p.TotalDays / 365));
if (p.Days % 365 > 30)
sb.AppendFormat("{0} months, ", (p.Days % 365) / 30);
if (p.Days % 365 % 30 > 7)
sb.AppendFormat("{0} weeks, ", p.Days % 365 % 30 / 7);
if (p.Days % 365 % 30 % 7 > 0)
sb.AppendFormat("{0} days, ", p.Days % 365 % 30 % 7);
if (p.Hours > 0)
sb.AppendFormat("{0} hrs, ", p.Hours);
if (p.Minutes > 0)
sb.AppendFormat("{0} mins, ", p.Minutes);
if (p.Seconds > 0)
sb.AppendFormat("{0} seconds ,", p.Seconds);
if (sb.Length > 2)
{
sb.Remove(sb.Length - 2, 2); // remove the last ", " part.
}
return sb.ToString();
}
What’s with the
% 59? Minutes contains the number of minutes, and Seconds contains the number of seconds. That should be all you need.If you had to calculate the remainder, shouldn’t it be
% 60? But why is that needed here at all?Note that TotalMinutes and TotalSeconds contain the number of minutes and seconds in the entire time span. But Minutes and Seconds just return the minutes and seconds portion of that time span.