转换时间为 “XX分钟之前”

public static string getTimeAgo(string strDate)

    {

        string strTime = string.Empty;

        if (clsCommon.IsDate(strDate))

        {

            TimeSpan t = DateTime.UtcNow - Convert.ToDateTime(strDate);

            double deltaSeconds = t.TotalSeconds;



            double deltaMinutes = deltaSeconds / 60.0f;

            int minutes;



            if (deltaSeconds < 5)

            {

                return "当前";

            }

            else if (deltaSeconds < 60)

            {

                return Math.Floor(deltaSeconds) + " 秒之前";

            }

            else if (deltaSeconds < 120)

            {

                return "分钟之前";

            }

            else if (deltaMinutes < 60)

            {

                return Math.Floor(deltaMinutes) + " minutes ago";

            }

            else if (deltaMinutes < 120)

            {

                return "An hour ago";

            }

            else if (deltaMinutes < (24 * 60))

            {

                minutes = (int)Math.Floor(deltaMinutes / 60);

                return minutes + " hours ago";

            }

            else if (deltaMinutes < (24 * 60 * 2))

            {

                return "Yesterday";

            }

            else if (deltaMinutes < (24 * 60 * 7))

            {

                minutes = (int)Math.Floor(deltaMinutes / (60 * 24));

                return minutes + " days ago";

            }

            else if (deltaMinutes < (24 * 60 * 14))

            {

                return "Last week";

            }

            else if (deltaMinutes < (24 * 60 * 31))

            {

                minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 7));

                return minutes + " weeks ago";

            }

            else if (deltaMinutes < (24 * 60 * 61))

            {

                return "Last month";

            }

            else if (deltaMinutes < (24 * 60 * 365.25))

            {

                minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 30));

                return minutes + " months ago";

            }

            else if (deltaMinutes < (24 * 60 * 731))

            {

                return "Last year";

            }



            minutes = (int)Math.Floor(deltaMinutes / (60 * 24 * 365));

            return minutes + " years ago";

        }

        else

        {

            return "";

        }

    }
调用方法:
getTimeAgo(DateTime.Now.ToString());

你可能感兴趣的:(转换)