使用时间跨度显示总持续时间
本文关键字:持续时间 显示 时间跨度 | 更新日期: 2023-09-27 18:30:21
我需要帮助来显示这样的东西。如果您认识到,这是我们的Stack Overflow帐户的会员期限。
member for 2 years, 3 months
我需要稍微修改一下。我的条件和显示格式如下:
Conditions Display Format
1. Below 7 days -> Days (5 days)
2. 7 -30 days -> Weeks, days ( 2 Weeks, 3 Days) in case of 17 days.
3. 30 - 365 -> Months, Weeks, Days (4Months, 2Weeks, 2Days) in case of 136 days
3. 365 or grter -> Years, Months only (2years, 3 Months)
What I did:
我正在使用时间跨度来获取日期差异。但我对我的输出不满意。我有一个会员,其注册将在 2-2 天后达到 3 个月大,但仍然在 1 个月前显示。这是我的代码:
TimeSpan ts = DateTime.Now - Convert.ToDateTime(company.RegistrationDate);
if (ts.Days > 365)
membersince.InnerText = string.Format("{0} years", (ts.Days / 365));
else if (ts.Days < 30)
membersince.InnerText = string.Format("{0} days", ts.Days);
else if(ts.Days > 30)
membersince.InnerText = string.Format("{0} months", (ts.Days/30));
可以使用
.NET 的时间段库的 DateDiff 类:
// ----------------------------------------------------------------------
public void DateDiffSample()
{
DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
Console.WriteLine( "Date1: {0}", date1 );
// > Date1: 08.11.2009 07:13:59
DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
Console.WriteLine( "Date2: {0}", date2 );
// > Date2: 20.03.2011 19:55:28
DateDiff dateDiff = new DateDiff( date1, date2 );
// elapsed
Console.WriteLine( "DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears );
// > DateDiff.ElapsedYears: 1
Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
// > DateDiff.ElapsedMonths: 4
Console.WriteLine( "DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays );
// > DateDiff.ElapsedDays: 12
Console.WriteLine( "DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours );
// > DateDiff.ElapsedHours: 12
Console.WriteLine( "DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes );
// > DateDiff.ElapsedMinutes: 41
Console.WriteLine( "DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds );
// > DateDiff.ElapsedSeconds: 29
} // DateDiffSample
为什么这是一个问题?如果一个人的年龄是 57 天,那么57 / 30 == 1
.如果您希望从中2
,那么您应该
1)确保除法的结果是双倍的(只需除以30.0
而不是30
)。原因是int / int
也是int
。
简单测试:(57 / 30).GetType().Name == Int32
(57 / 30.0).GetType().Name == Double
还(int)1.9 == 1
.
2)Math.Round()
结果。这将是实际的数学舍入,而不是只是删除小数点后的所有内容。
只需使用TotalDays
,它将为您提供双倍(并且在政治上更正确,因为Days
只是组件,尽管在您的情况下是相似的),并且Math.Round()
正如@Ilia已经建议的那样"最近"。
Math.Round(ts.TotalDays);
。或
String.Format("{0:0.##}", ts.TotalDays);
。这应该给你喜欢1.7 months
(正如我从我的脑海中回忆的那样或测试你的获胜格式)
这更像是一个逻辑和用户界面问题,如何"呈现"给用户。
编辑:仅使用TotalDays
格式化为1.7
- 或使用Math.Round获得圆形月份
static public string calculateAge(DateTime birthDate, DateTime now)
{
birthDate = birthDate.Date;
now = now.Date;
var days = now.Day - birthDate.Day;
if (days < 0)
{
var newNow = now.AddMonths(-1);
days += (int)(now - newNow).TotalDays;
now = newNow;
}
var weeks = days / 7;
days = days - weeks * 7;
var months = now.Month - birthDate.Month;
if (months < 0)
{
months += 12;
now = now.AddYears(-1);
}
var years = now.Year - birthDate.Year;
if (years > 0)
{
if (months > 0)
return string.Format("{0} Years, {1} Months", years, months);
else
return string.Format("{0} Years", years);
}
if (months > 0)
{
var builder = new StringBuilder();
builder.AppendFormat("{0} Months", months);
if (weeks > 0)
builder.AppendFormat(", {0} Weeks", weeks);
if (days > 0)
builder.AppendFormat(", {0} Days", days);
return builder.ToString();
}
if (weeks > 0)
{
if (days > 0)
return string.Format("{0} Weeks, {1} Days", weeks, days);
return string.Format("{0} Weeks", weeks);
}
return string.Format("{0} Days", days);
}