Skip to content

Commit 83ebed5

Browse files
committed
simplify friendly date string
1 parent 771aaa5 commit 83ebed5

File tree

2 files changed

+21
-156
lines changed

2 files changed

+21
-156
lines changed

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private async Task AddFolder(StorageFolder folder, string pageName, Cancellation
309309
_classicFolderList.Add(new Classic_ListedFolderItem()
310310
{
311311
FileName = folder.Name,
312-
FileDate = ListedItem.GetFriendlyDate(basicProperties.DateModified.LocalDateTime),
312+
FileDate = ListedItem.GetFriendlyDate(basicProperties.DateModified),
313313
FileExtension = "Folder",
314314
FilePath = folder.Path
315315
});

Files UWP/Filesystem/ListedItem.cs

Lines changed: 20 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public DateTimeOffset FileDateReal
2424
get { return _fileDataReal; }
2525
set
2626
{
27-
FileDate = GetFriendlyDate(value.LocalDateTime);
27+
FileDate = GetFriendlyDate(value);
2828
_fileDataReal = value;
2929
}
3030
}
@@ -36,164 +36,29 @@ public ListedItem(string folderRelativeId)
3636
FolderRelativeId = folderRelativeId;
3737
}
3838

39-
public static string GetFriendlyDate(DateTime d)
39+
public static string GetFriendlyDate(DateTimeOffset d)
4040
{
41-
if (d.Year == DateTime.Now.Year) // If item is accessed in the same year as stored
42-
{
43-
if (d.Month == DateTime.Now.Month) // If item is accessed in the same month as stored
44-
{
45-
if ((DateTime.Now.Day - d.Day) < 7) // If item is accessed on the same week
46-
{
47-
if (d.DayOfWeek == DateTime.Now.DayOfWeek) // If item is accessed on the same day as stored
48-
{
49-
if ((DateTime.Now.Hour - d.Hour) > 1)
50-
{
51-
return DateTime.Now.Hour - d.Hour + " hours ago";
52-
}
53-
else
54-
{
55-
return DateTime.Now.Minute - d.Minute + " hour ago";
56-
}
57-
}
58-
else // If item is from a previous day of the same week
59-
{
60-
return d.DayOfWeek + " at " + d.ToShortTimeString();
61-
}
62-
}
63-
else // If item is from a previous week of the same month
64-
{
65-
string monthAsString = "Month";
66-
switch (d.Month)
67-
{
68-
case 1:
69-
monthAsString = "January";
70-
break;
71-
case 2:
72-
monthAsString = "February";
73-
break;
74-
case 3:
75-
monthAsString = "March";
76-
break;
77-
case 4:
78-
monthAsString = "April";
79-
break;
80-
case 5:
81-
monthAsString = "May";
82-
break;
83-
case 6:
84-
monthAsString = "June";
85-
break;
86-
case 7:
87-
monthAsString = "July";
88-
break;
89-
case 8:
90-
monthAsString = "August";
91-
break;
92-
case 9:
93-
monthAsString = "September";
94-
break;
95-
case 10:
96-
monthAsString = "October";
97-
break;
98-
case 11:
99-
monthAsString = "November";
100-
break;
101-
case 12:
102-
monthAsString = "December";
103-
break;
104-
}
105-
return monthAsString + " " + d.Day;
106-
}
41+
var elapsed = DateTimeOffset.Now - d;
10742

108-
}
109-
else // If item is from a past month of the same year
110-
{
111-
string monthAsString = "Month";
112-
switch (d.Month)
113-
{
114-
case 1:
115-
monthAsString = "January";
116-
break;
117-
case 2:
118-
monthAsString = "February";
119-
break;
120-
case 3:
121-
monthAsString = "March";
122-
break;
123-
case 4:
124-
monthAsString = "April";
125-
break;
126-
case 5:
127-
monthAsString = "May";
128-
break;
129-
case 6:
130-
monthAsString = "June";
131-
break;
132-
case 7:
133-
monthAsString = "July";
134-
break;
135-
case 8:
136-
monthAsString = "August";
137-
break;
138-
case 9:
139-
monthAsString = "September";
140-
break;
141-
case 10:
142-
monthAsString = "October";
143-
break;
144-
case 11:
145-
monthAsString = "November";
146-
break;
147-
case 12:
148-
monthAsString = "December";
149-
break;
150-
}
151-
return monthAsString + " " + d.Day;
152-
}
43+
if (elapsed.TotalDays > 7)
44+
{
45+
return d.ToString("D");
46+
}
47+
else if (elapsed.TotalDays > 1)
48+
{
49+
return $"{elapsed.Days} days ago";
50+
}
51+
else if (elapsed.TotalHours > 1)
52+
{
53+
return $"{elapsed.Hours} hours ago";
54+
}
55+
else if (elapsed.TotalMinutes > 1)
56+
{
57+
return $"{elapsed.Minutes} minutes ago";
15358
}
154-
else // If item is from a past year
59+
else
15560
{
156-
string monthAsString = "Month";
157-
switch (d.Month)
158-
{
159-
case 1:
160-
monthAsString = "January";
161-
break;
162-
case 2:
163-
monthAsString = "February";
164-
break;
165-
case 3:
166-
monthAsString = "March";
167-
break;
168-
case 4:
169-
monthAsString = "April";
170-
break;
171-
case 5:
172-
monthAsString = "May";
173-
break;
174-
case 6:
175-
monthAsString = "June";
176-
break;
177-
case 7:
178-
monthAsString = "July";
179-
break;
180-
case 8:
181-
monthAsString = "August";
182-
break;
183-
case 9:
184-
monthAsString = "September";
185-
break;
186-
case 10:
187-
monthAsString = "October";
188-
break;
189-
case 11:
190-
monthAsString = "November";
191-
break;
192-
case 12:
193-
monthAsString = "December";
194-
break;
195-
}
196-
return monthAsString + " " + d.Day + ", " + d.Year;
61+
return $"{elapsed.Seconds} seconds ago";
19762
}
19863
}
19964
}

0 commit comments

Comments
 (0)