asp.Netフォーマット表示時間は数ヶ月、数日前、数時間前、数分前、または数秒前

6873 ワード

 1:  /// <summary>
 2:          ///            ,   ,    ,    ,    
 3:          /// </summary>
 4:          /// <param name="dt">         </param>
 5:          /// <returns>   ,   ,    ,    ,    </returns>
 6:          public static string DateStringFromNow(DateTime dt)
 7:          {
 8:              TimeSpan span = DateTime.Now - dt;
 9:              if (span.TotalDays > 60)
10:              {
11:                  return dt.ToShortDateString();
12:              }
13:              else if (span.TotalDays > 30)
14:              {
15:                  return "1   ";
16:              }
17:              else if (span.TotalDays > 14)
18:              {
19:                  return "2  ";
20:              }
21:              else if (span.TotalDays > 7)
22:              {
23:                  return "1  ";
24:              }
25:              else if (span.TotalDays > 1)
26:              {
27:                  return string.Format("{0}  ", (int)Math.Floor(span.TotalDays));
28:              }
29:              else if (span.TotalHours > 1)
30:              {
31:                  return string.Format("{0}   ", (int)Math.Floor(span.TotalHours));
32:              }
33:              else if (span.TotalMinutes > 1)
34:              {
35:                  return string.Format("{0}   ", (int)Math.Floor(span.TotalMinutes));
36:              }
37:              else if (span.TotalSeconds >= 1)
38:              {
39:                  return string.Format("{0}  ", (int)Math.Floor(span.TotalSeconds));
40:              }
41:              else
42:              {
43:                  return "1  ";
44:              }
45:          }
46:   

.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}
.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}
.codearea pre.alt{ background-color:#f7f7ff !important}
.codearea .lnum{color:#4f81bd;line-height:18px}