Asp.NET時間Since変換

26156 ワード

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);
}