判断时间

有时候我们需要判断时间是多久,比如做贴吧,别人多久回复你的,刚刚、几小时前、一天前等等,大神请路过

public string timeSpan(string time)

    {

        System.DateTime pauseT = System.Convert.ToDateTime(time);

        System.DateTime resumeT = System.DateTime.Now;

        System.TimeSpan ts1 = new System.TimeSpan(pauseT.Ticks);

        System.TimeSpan ts2 = new System.TimeSpan(resumeT.Ticks);

        System.TimeSpan tsSub = ts1.Subtract(ts2).Duration();

        string ll = "";

        if (tsSub.Days == 0 && tsSub.Hours == 0)

        {

            ll = "刚刚";

        }

        else if (tsSub.Days == 0 && tsSub.Hours > 0)

        {

            if (tsSub.Hours > 24)

            {

                ll = tsSub.Hours % 24 + "天前";

            }

            else

            {

 

                ll = tsSub.Hours + "小时前";

            }

        }

        else if (tsSub.Days > 0)

        {

            if (tsSub.Days > 365)

            {

 

                ll = "一年前";

            }

            else if (tsSub.Days > 30)

            {

                ll = "一月前";

            }

            else

            {

                ll = tsSub.Days + "天前";

            }

        }

        return ll;

    }

posted on 2018-05-07 09:20  编程流氓  阅读(186)  评论(0编辑  收藏  举报

导航