我只是想利用这个TimePeriod库。我刚发现了一些我不确定是正确的东西,但我无法将其标记为错误。只需要澄清一下。
Dim xDateDiff As New DateDiff(New Date(2011, 1, 1), New Date(2011, 2, 1))
Console.WriteLine("Week Count : " + xDateDiff.Weeks().ToString()
现在,周计数输出为5,如果您看到公历,则两个日期之间为6周。周数是从零开始的吗?这令我不安。
请帮忙。
答案 0 :(得分:1)
我从未使用过TimePeriod库,但这不仅仅是两个日期之间存在5周的差异,但如果包含第一周,则有6周的时间吗?我的观点是,你不能只做xDateDiff.Weeks() + 1
吗?
答案 1 :(得分:1)
DateDiff 类旨在表示两个日期的UI友好差异。 周属性仅计算已完成的周数。
如果要计算两个日期之间所涉及的周数,您可以使用以下代码:
// ----------------------------------------------------------------------
public void InvolvedWeekCount()
{
Console.WriteLine( "Involved week count: " +
CalcInvolvedWeekCount( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 1, 2 ), DayOfWeek.Sunday ) );
// > Involved week count: 2
Console.WriteLine( "Involved week count: " +
CalcInvolvedWeekCount( new DateTime( 2011, 1, 2 ), new DateTime( 2011, 1, 8 ), DayOfWeek.Sunday ) );
// > Involved week count: 1
Console.WriteLine( "Involved week count: " +
CalcInvolvedWeekCount( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 1, 29 ), DayOfWeek.Sunday ) );
// > Involved week count: 5
Console.WriteLine( "Involved week count: " +
CalcInvolvedWeekCount( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 1, 30 ), DayOfWeek.Sunday ) );
// > Involved week count: 6
Console.WriteLine( "Involved week count: " +
CalcInvolvedWeekCount( new DateTime( 2011, 1, 1 ), new DateTime( 2011, 2, 1 ), DayOfWeek.Sunday ) );
// > Involved week count: 6
} // InvolvedWeekCount
// ----------------------------------------------------------------------
private int CalcInvolvedWeekCount( DateTime date1, DateTime date2, DayOfWeek firstDayOfWeek )
{
if ( date1.Date.Equals( date2.Date ) )
{
return 0;
}
DateTime startWeek = TimeTool.GetStartOfWeek( date1, firstDayOfWeek );
DateTime endWeek = TimeTool.GetStartOfWeek( date2, firstDayOfWeek ).AddDays( TimeSpec.DaysPerWeek );
return (int)( endWeek.Subtract( startWeek ).TotalDays / TimeSpec.DaysPerWeek );
} // CalcInvolvedWeekCount
答案 2 :(得分:0)
你的问题假设是虚假的。 2月1日是在1月1日之后的31天。31/7只是不到4个半月,所以2月1日是在1月1日之后的第五周。你如何得出6作为预期答案?