方法一: 使用 toolkit的 RelativeTimeConverter,使用方式
<phone:PhoneApplicationPage.Resources> <toolkit:RelativeTimeConverter x:Key="RelativeTimeConverter"/> </phone:PhoneApplicationPage.Resources> <ListBox> ...... <TextBlock Text="{Binding CreateTime, Converter={StaticResource RelativeTimeConverter}}" /> ...... </ListBox>
方法二:自己写格式化时间的类
public string DateStringFromNow(DateTime dt)
{
TimeSpan span = DateTime.Now - dt;
if (span.TotalDays >= 365)
{
int number = (int)(span.TotalDays / 365);
return number + "年前";
}
else if (span.TotalDays >= 30)
{
int number = (int)(span.TotalDays / 30);
return number + "月前";
}
else
{
if (span.TotalDays > 7)
{
int number = (int)(span.TotalDays / 7);
return number + "周前";
}
else if (span.TotalDays > 1)
{
return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
}
else if (span.TotalHours > 1)
{
return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
}
else if (span.TotalMinutes > 1)
{
return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
}
else if (span.TotalSeconds >= 1)
{
return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
}
else
{
return "刚刚";
}
}
}