zoukankan      html  css  js  c++  java
  • Asp.NET 时间Since转换

    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);
    }
  • 相关阅读:
    iframe,table,window.open求救问题
    你的明星臉~~哈哈~~~(要附正面照片哦==)
    DataGrid的表頭排序問題(GridView雷同了啦)
    致歉(TO师傅)
    程式設計師的著裝(哈哈哈~~~)
    SQL(top与group by)求助
    MySql与超级终端
    hdu 1061 Rightmost Digit
    hdu 2669 Romantic
    poj 1061 青蛙的约会
  • 原文地址:https://www.cnblogs.com/Leung/p/1553604.html
Copyright © 2011-2022 走看看