summaryrefslogtreecommitdiff
path: root/Juick/Classes/DateHelper.cs
blob: 072c56a15b2f885678441f74cc64fe3725f92f04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;

namespace Juick.Classes
{
    public class DateHelper
    {
        public static string PrettyDate(DateTime dateTime)
        {
            var timeSpan = DateTime.Now - dateTime;

            // span is less than or equal to 60 seconds, measure in seconds.
            if (timeSpan <= TimeSpan.FromSeconds(60))
            {
                return timeSpan.Seconds > 5
                    ? "about " + timeSpan.Seconds + " seconds ago"
                    : "just now";
            }

            // span is less than or equal to 60 minutes, measure in minutes.
            if (timeSpan <= TimeSpan.FromMinutes(60))
            {
                return timeSpan.Minutes > 1
                    ? "about " + timeSpan.Minutes + " minutes ago"
                    : "about a minute ago";
            }

            // span is less than or equal to 24 hours, measure in hours.
            if (timeSpan <= TimeSpan.FromHours(24))
            {
                return timeSpan.Hours > 1
                    ? "about " + timeSpan.Hours + " hours ago"
                    : "about an hour ago";
            }

            // span is less than or equal to 30 days (1 month), measure in days.
            if (timeSpan <= TimeSpan.FromDays(30))
            {
                return timeSpan.Days > 1
                    ? "about " + timeSpan.Days + " days ago"
                    : "about a day ago";
            }

            // span is less than or equal to 365 days (1 year), measure in months.
            if (timeSpan <= TimeSpan.FromDays(365))
            {
                return timeSpan.Days > 30
                    ? "about " + timeSpan.Days / 30 + " months ago"
                    : "about a month ago";
            }

            // span is greater than 365 days (1 year), measure in years.
            return timeSpan.Days > 365
                ? "about " + timeSpan.Days / 365 + " years ago"
                : "about a year ago";
        }
    }
}