首頁  >  文章  >  後端開發  >  C# 顯示過去了多久的函數

C# 顯示過去了多久的函數

黄舟
黄舟原創
2017-03-01 10:40:331540瀏覽

C#  顯示過去了多久的函數

public string HowLongBeforeNow
        {
            get
            {
                var ts = new TimeSpan(DateTime.UtcNow.Ticks - CreatedDate.Ticks);
                var delta = ts.TotalSeconds;


                const int second = 1;
                const int minute = 60 * second;
                const int hour = 60 * minute;
                const int day = 24 * hour;
                const int month = 30 * day;
                if (delta < 0)
                {
                    throw new InvalidDataException("The CreatedDate can not be gather than today.");
                }
                if (delta < 1 * minute)
                {
                    return ts.Seconds == 1 ? "1 second ago" : ts.Seconds + " seconds ago";
                }
                if (delta < 2 * minute)
                {
                    return "1 minute ago";
                }
                if (delta < 45 * minute)
                {
                    return ts.Minutes + " minutes ago";
                }
                if (delta < 90 * minute)
                {
                    return "1 hour ago";
                }
                if (delta < 24 * hour)
                {
                    return ts.Hours + " hours ago";
                }
                if (delta < 48 * hour)
                {
                    return "yesterday";
                }
                if (delta < 30 * day)
                {
                    return ts.Days + " days ago";
                }
                if (delta < 12 * month)
                {
                    var months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
                    return months <= 1 ? "1 month ago" : months + " months ago";
                }
                var years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
                return years <= 1 ? "1 year ago" : years + " years ago";
            }
        }

css:

.how-long-ago {
        font-style: italic;
        color: #777;
    }


 以上就是C#  顯示過去了多久的函數的內容,更多相關內容請關注PHP中文網(www.php.cn)!


#
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn