本文介绍了季度日期的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算从开始日期到下一个日期的一年中的季度,例如第一个季度是从4月到6月,所以季度的第一个日期和季度的最后一个日期
请帮忙.
在此先感谢.
how to calculate the quarter of the year from the starting date to Next Date like first Quarter is Start from April to June so the first date of quarter and the Last Date of Quarter
Please help.
thanks in advance.
推荐答案
int GetQuarterName(DateTime myDate)
{
return Math.Ceiling(myDate.Month / 3);
}
DateTime GetQuarterStartingDate(DateTime myDate)
{
return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
}
// ----------------------------------------------------------------------
public static void ShowQuarterInfo()
{
ShowQuarterInfo( DateTime.Now );
} // ShowQuarterInfo
// ----------------------------------------------------------------------
public static void ShowQuarterInfo( DateTime moment )
{
// set start month to April
TimeCalendar calendar = new TimeCalendar(
new TimeCalendarConfig { YearBaseMonth = YearMonth.April } );
// working quarter
Quarter quarter = new Quarter( moment, calendar );
Console.WriteLine( "Quarter start: " + quarter.FirstDayStart );
Console.WriteLine( "Quarter end: " + quarter.LastDayStart );
// previous quarter
Quarter previousQuarter = quarter.GetPreviousQuarter();
Console.WriteLine( "Previous Quarter start: " + previousQuarter.FirstDayStart );
Console.WriteLine( "Previous Quarter end: " + previousQuarter.LastDayStart );
// next quarter
Quarter nextQuarter = quarter.GetNextQuarter();
Console.WriteLine( "Next Quarter start: " + nextQuarter.FirstDayStart );
Console.WriteLine( "Next Quarter end: " + nextQuarter.LastDayStart );
} // ShowQuarterInfo
另请参见此 [ ^ ]问题/答案.
您可以在文章 .NET的时间段库 [ ^ ].
干杯,加尼(Jani).
See also this[^] question/answer.
You will find more samples in the article Time Period Library for .NET[^].
Cheers, Jani.
private void make(int year, int quarter)
{
DateTime starting, ending;
int month = (4 + 3 * (quarter - 1)) % 12;
starting = new DateTime(year, month, 1);
ending = starting.AddMonths(3).AddDays(-1);
MessageBox.Show(starting.ToString() + "," + ending.ToString());
}
make(2010, 1);
make(2010, 2);
make(2010, 3);
make(2010, 4);
这篇关于季度日期的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!