ASP.NET 时间 Since 转换

目前 Web 2.0 的网站上很流行以:2秒前、3天前、1月前 这样的方式来显示一条消息的时间,这种做法想 Twitter饭否…等网站都在使用,用在即时性高的文章或消息上面。

我这里写了一个 C# 的实现方式:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
public static string Since(System.DateTime now, System.DateTime last)
{
    string strResult = "";

    try
    {
        TimeSpan ts = new TimeSpan();
        ts = now.Subtract(last);
        string strNumber = "";

        if (ts.TotalSeconds <= 30)
        {
            //30秒以前
            strResult = "刚刚";
        }
        else if (ts.TotalSeconds <= 59)
        {
            //小于1分钟
            strResult = "1分钟前";
        }
        else if (ts.TotalMinutes <= 60 && ts.TotalMinutes != 30)
        {
            //分钟,没有处理30分的时候
            strNumber = Convert.ToInt32(ts.TotalMinutes).ToString();
            if (ts.TotalMinutes == 2)
            {
                strNumber = "两";
            }

            strResult = strNumber + "分钟前";
        }
        else if (ts.TotalMinutes == 30)
        {
            //半个小时
            strResult = "半小时前";
        }
        else if (ts.TotalHours <= 23)
        {
            //小时
            strNumber = Convert.ToInt32(ts.TotalHours).ToString();
            if (ts.TotalHours == 2)
            {
                strNumber = "两";
            }

            strResult = ts.Hours + "小时前";
        }
        else if (ts.TotalHours == 24)
        {
            //刚好一天
            strResult = "一天前";
        }
        else if (ts.TotalDays < 30 && ts.TotalDays != 15)
        {
            //半个月以前
            strNumber = Convert.ToInt32(ts.TotalDays).ToString();
            if (ts.TotalDays == 2)
            {
                strNumber = "两";
            }

            strResult = strNumber + "天前";
        }
        else if (ts.TotalDays == 15)
        {
            //半月
            strResult = "半个月前";
        }
        else if (ts.TotalDays <= 364)
        {
            //按月的
            int month = Convert.ToInt32(ts.TotalDays) / 30;
            strNumber = month.ToString();
            if (month == 2)
            {
                strNumber = "两";
            }
            strResult = strNumber + "个月前";
        }
        else if (ts.TotalDays <= 547)
        {
            //一年半以下的
            strResult = "一年前";
        }
        else if (ts.TotalDays <= 910)
        {
            //两年半以下的
            strResult = "两年前";
        }
        else
        {
            strResult = last.ToString("yy年M月d日");
        }
    }
    catch (Exception ex)
    {
        YtRuntime.WriteLog(ex.ToString());
        strResult = last.ToString("yy年M月d日");
    }
    return strResult;
}

public static string Since(System.DateTime last)
{
    return Since(System.DateTime.Now, last);
}

.NET

Comments