mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-23 00:38:47 +00:00
🔨 Add meaningful implementation of Ago()
Ago() now returns a human-readable string instead of a DateTime
This commit is contained in:
parent
1712dbaa02
commit
cd04c1b66f
@ -14,11 +14,42 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calculates how long ago a specified <see cref="TimeSpan"/> was.
|
/// Calculates how long ago a specified <see cref="TimeSpan"/> was.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="span">The <see cref="TimeSpan"/>.</param>
|
/// <param name="span">The <see cref="TimeSpan"/>. Defaults to <see langword="false"/>.</param>
|
||||||
/// <param name="utc">Optional. Whether or not to use <see cref="DateTime.UtcNow"/> instead of <see cref="DateTime.Now"/>.
|
/// <returns>Returns a human-readable <see cref="String"/> describing how long ago <paramref name="span"/>
|
||||||
/// Defaults to <see langword="false"/>.</param>
|
/// represents from now.</returns>
|
||||||
/// <returns>Returns a <see cref="DateTime"/>.</returns>
|
public static string Ago(this TimeSpan span)
|
||||||
public static DateTime Ago(this TimeSpan span, bool utc = false) =>
|
{
|
||||||
(utc ? DateTime.UtcNow : DateTime.Now) - span;
|
if (span < TimeSpan.FromSeconds(60))
|
||||||
|
{
|
||||||
|
return $"{span.Seconds} seconds ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span < TimeSpan.FromMinutes(60))
|
||||||
|
{
|
||||||
|
return span.Minutes > 1 ? $"about {span.Minutes} minutes ago" : "about a minute ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span < TimeSpan.FromHours(24))
|
||||||
|
{
|
||||||
|
return span.Hours > 1 ? $"about {span.Hours} hours ago" : "about an hour ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span <= TimeSpan.FromDays(7))
|
||||||
|
{
|
||||||
|
return span.Days > 1 ? $"about {span.Days} days ago" : "yesterday";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span < TimeSpan.FromDays(30))
|
||||||
|
{
|
||||||
|
return $"about {span.Days} days ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (span < TimeSpan.FromDays(365))
|
||||||
|
{
|
||||||
|
return span.Days > 30 ? $"about {span.Days} months ago" : "about a month ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
return span.Days > 365 ? $"about {span.Days / 365} years ago" : "about a year ago";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user