將時間處理成類似于“3分前”,“1小時前“的C#代碼

jopen 12年前發布 | 808 次閱讀

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;

namespace MicroBlogForWP7.Classes { public class TimeParser { /// <summary> /// 把秒轉換成分鐘 /// </summary> /// <returns></returns> public static int SecondToMinute(int Second) { decimal mm = (decimal)((decimal)Second / (decimal)60); return Convert.ToInt32(Math.Ceiling(double.Parse(mm.ToString()))); }

    #region 返回某年某月最后一天
    /// <summary>
    /// 返回某年某月最后一天
    /// </summary>
    /// <param name="year">年份</param>
    /// <param name="month">月份</param>
    /// <returns>日</returns>
    public static int GetMonthLastDate(int year, int month)
    {
        DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
        int Day = lastDay.Day;
        return Day;
    }
    #endregion

    #region 返回時間差
    public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
    {
        string dateDiff = null;
        try
        {
            //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            //TimeSpan ts = ts1.Subtract(ts2).Duration();
            TimeSpan ts = DateTime2 - DateTime1;
            if (ts.Days >= 1)
            {
                dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
            }
            else
            {
                if (ts.Hours > 1)
                {
                    dateDiff = ts.Hours.ToString() + "小時前";
                }
                else
                {
                    dateDiff = ts.Minutes.ToString() + "分鐘前";
                }
            }
        }
        catch
        { }
        return dateDiff;
    }
    #endregion

    #region 返回該微博是什么時候發的
    /// <summary>
    /// 返回該微博是什么時候發的
    /// </summary>
    /// <param name="year">發微博時間</param>
    /// <param name="month">當前時間</param>
    public static string GetWhenSendTime(DateTime BlogTime, DateTime NowTime)
    {
        DateTime t1 = BlogTime;
        DateTime t2 = NowTime;
        string RES = "";
        if ((t2 - t1).TotalMinutes > 1440)
        {
            RES = t1.ToString("yyyy年MM月dd日 HH:mm");
        }
        else
            if ((t2 - t1).TotalMinutes > 60)
            {
                RES = Math.Floor(((t2 - t1).TotalMinutes / 60)).ToString() + "小時" + (Math.Floor((t2 - t1).TotalMinutes) % 60).ToString() + "分鐘前";
            }
            else
            {
                if ((Math.Floor((t2 - t1).TotalMinutes) % 60) <= 0) RES = "剛剛更新";
                else
                    RES = (Math.Floor((t2 - t1).TotalMinutes) % 60).ToString() + "分鐘前";
            }
        return RES;
    }
    #endregion
}

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!