Skip to content

Commit 74277cf

Browse files
Brian Canzanellabcanzanella
Brian Canzanella
authored andcommitted
updates timeago function for VS
1 parent b373bb4 commit 74277cf

File tree

1 file changed

+38
-64
lines changed

1 file changed

+38
-64
lines changed
Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,45 @@
11
using System;
22

3-
namespace CodeStream.VisualStudio.Core.Extensions
4-
{
5-
public static class DateTimeExtensions
6-
{
7-
public static DateTime FromLong(this long l)
8-
{
9-
return new DateTime(1970, 01, 01).AddMilliseconds(l);
10-
}
3+
namespace CodeStream.VisualStudio.Core.Extensions {
4+
public static class DateTimeExtensions {
5+
public static DateTime FromLong(this long l) {
6+
return new DateTime(1970, 01, 01).AddMilliseconds(l);
7+
}
118

12-
/// <summary>
13-
/// Converts a local time to human readable words
14-
/// </summary>
15-
/// <param name="dateTime"></param>
16-
/// <remarks>https://dotnetthoughts.net/time-ago-function-for-c/</remarks>
17-
/// <remarks>More crap here: https://stackoverflow.com/questions/11/calculate-relative-time-in-c-sharp</remarks>
18-
/// <returns></returns>
19-
public static string TimeAgo(this DateTime dateTime)
20-
{
21-
var timeSpan = DateTime.Now.Subtract(dateTime);
9+
/// <summary>
10+
/// Converts a local time to human readable words
11+
/// </summary>
12+
/// <param name="dateTime"></param>
13+
public static string TimeAgo(this DateTime dateTime) {
14+
var timeSpan = DateTime.Now.Subtract(dateTime);
2215

23-
if (timeSpan <= TimeSpan.FromSeconds(60))
24-
{
25-
return $"{timeSpan.Seconds} seconds ago";
26-
}
27-
if (timeSpan <= TimeSpan.FromMinutes(60))
28-
{
29-
return timeSpan.Minutes > 1 ? $"about {timeSpan.Minutes} minutes ago" : "about a minute ago";
30-
}
31-
if (timeSpan <= TimeSpan.FromHours(24))
32-
{
33-
return timeSpan.Hours > 1 ? $"about {timeSpan.Hours} hours ago" : "about an hour ago";
34-
}
35-
if (timeSpan <= TimeSpan.FromDays(30))
36-
{
37-
return timeSpan.Days > 1 ? $"about {timeSpan.Days} days ago" : "yesterday";
38-
}
39-
if (timeSpan <= TimeSpan.FromDays(365))
40-
{
41-
return timeSpan.Days > 30 ? $"about {timeSpan.Days / 30} months ago" : "about a month ago";
42-
}
16+
if (timeSpan <= TimeSpan.FromSeconds(60)) return $"{timeSpan.Seconds} seconds ago";
17+
if (timeSpan <= TimeSpan.FromMinutes(60)) return timeSpan.Minutes > 1 ? $"about {timeSpan.Minutes} minutes ago" : "about a minute ago";
18+
if (timeSpan <= TimeSpan.FromHours(24)) return timeSpan.Hours > 1 ? $"about {timeSpan.Hours} hours ago" : "about an hour ago";
19+
if (timeSpan <= TimeSpan.FromDays(30)) return timeSpan.Days > 1 ? $"about {timeSpan.Days} days ago" : "yesterday";
20+
if (timeSpan <= TimeSpan.FromDays(365)) return timeSpan.Days > 30 ? $"about {timeSpan.Days / 30} months ago" : "about a month ago";
21+
return timeSpan.Days > 365 ? $"about {timeSpan.Days / 365} years ago" : "about a year ago";
22+
}
4323

44-
return timeSpan.Days > 365 ? $"about {timeSpan.Days / 365} years ago" : "about a year ago";
45-
}
24+
public static string ToDisplayDate(this DateTime dt) {
25+
return string.Format("{0:MMMM d}{1}, {0:yyyy hh:mmtt}", dt, GetDaySuffix(dt.Day));
26+
}
4627

47-
public static string ToDisplayDate(this DateTime dt)
48-
{
49-
return string.Format("{0:MMMM d}{1}, {0:yyyy hh:mmtt}", dt, GetDaySuffix(dt.Day));
50-
}
51-
52-
private static string GetDaySuffix(int day)
53-
{
54-
switch (day)
55-
{
56-
case 1:
57-
case 21:
58-
case 31:
59-
return "st";
60-
case 2:
61-
case 22:
62-
return "nd";
63-
case 3:
64-
case 23:
65-
return "rd";
66-
default:
67-
return "th";
68-
}
69-
}
70-
}
28+
private static string GetDaySuffix(int day) {
29+
switch (day) {
30+
case 1:
31+
case 21:
32+
case 31:
33+
return "st";
34+
case 2:
35+
case 22:
36+
return "nd";
37+
case 3:
38+
case 23:
39+
return "rd";
40+
default:
41+
return "th";
42+
}
43+
}
44+
}
7145
}

0 commit comments

Comments
 (0)