1, C#版
/// <summary>
/// LanguageType=1 -->English ; 2 --> Chinese
/// </summary>
/// <param name="DateTime1"></param>
/// <param name="DateTime2"></param>
/// <param name="iLanguageType"></param>
/// <returns></returns>
public static string TimeDuration(DateTime DateTime1, DateTime DateTime2, int iLanguageType = 1)
{
string dateDiff = "";
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//if(iLanguageType==2)
// dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
//else if (iLanguageType == 1)
// dateDiff = ts.Days.ToString() + "Days" + ts.Hours.ToString() + "Hours" + ts.Minutes.ToString() + "Minutes" + ts.Seconds.ToString() + "Seconds";
if (iLanguageType == 2)
{
if (ts.Days > 0)
dateDiff += ts.Days.ToString() + " 天 ";
if (ts.Hours > 0)
dateDiff += ts.Hours.ToString() + " 小时 ";
if (ts.Minutes > 0)
dateDiff += ts.Minutes.ToString() + " 分钟 ";
if (ts.Seconds > 0)
dateDiff += ts.Seconds.ToString() + " 秒 ";
if (ts.Milliseconds > 0)
dateDiff += ts.Milliseconds.ToString() + " 毫秒 ";
}
else if (iLanguageType == 1)
{
if (ts.Days > 0)
dateDiff += ts.Days.ToString() + " Days ";
if (ts.Hours > 0)
dateDiff += ts.Hours.ToString() + " Hours ";
if (ts.Minutes > 0)
dateDiff += ts.Minutes.ToString() + " Minutes ";
if (ts.Seconds > 0)
dateDiff += ts.Seconds.ToString() + " Seconds ";
if (ts.Milliseconds > 0)
dateDiff += ts.Milliseconds.ToString() + " Miniseconds ";
}
return dateDiff;
}
2,VB.NET版
'/// <summary>
' /// LanguageType=1 -->English ; 2 --> Chinese
' /// </summary>
' /// <param name="DateTime1"></param>
' /// <param name="DateTime2"></param>
' /// <param name="iLanguageType"></param>
' /// <returns></returns>
Public Function TimeDuration(ByVal DateTime1 As DateTime, ByVal DateTime2 As DateTime, Optional ByVal iLanguageType As Integer = 1) As String
Dim dateDiffResult As String = ""
Dim ts1 As TimeSpan = New TimeSpan(DateTime1.Ticks)
Dim ts2 As TimeSpan = New TimeSpan(DateTime2.Ticks)
Dim ts As TimeSpan = ts1.Subtract(ts2).Duration()
' //if(iLanguageType==2)
' // dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
' //else if (iLanguageType == 1)
' // dateDiff = ts.Days.ToString() + "Days" + ts.Hours.ToString() + "Hours" + ts.Minutes.ToString() + "Minutes" + ts.Seconds.ToString() + "Seconds";
If (iLanguageType = 2) Then
If (ts.Days > 0) Then
dateDiffResult += ts.Days.ToString() + " 天 "
End If
If (ts.Hours > 0) Then
dateDiffResult += ts.Hours.ToString() + " 小时 "
End If
If (ts.Minutes > 0) Then
dateDiffResult += ts.Minutes.ToString() + " 分钟 "
End If
If (ts.Seconds > 0) Then
dateDiffResult += ts.Seconds.ToString() + " 秒 "
End If
If (ts.Milliseconds > 0) Then
dateDiffResult += ts.Milliseconds.ToString() + " 毫秒 "
End If
ElseIf (iLanguageType = 1) Then
If (ts.Days > 0) Then
dateDiffResult += ts.Days.ToString() + " Days "
End If
If (ts.Hours > 0) Then
dateDiffResult += ts.Hours.ToString() + " Hours "
End If
If (ts.Minutes > 0) Then
dateDiffResult += ts.Minutes.ToString() + " Minutes "
End If
If (ts.Seconds > 0) Then
dateDiffResult += ts.Seconds.ToString() + " Seconds "
End If
If (ts.Milliseconds > 0) Then
dateDiffResult += ts.Milliseconds.ToString() + " Miniseconds "
End If
End If
Return dateDiffResult
End Function
http://www.blogjava.net/AndyZhang/archive/2012/05/02/377157.html