季度日期的计算 [英] Calculation of Quarter Date

查看:167
本文介绍了季度日期的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算从开始日期到下一个日期的一年中的季度,例如第一个季度是从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.

推荐答案

您好

我没有检查此代码,请检查此代码..

Hi

i didnt check this code, pls check this code..

int GetQuarterName(DateTime myDate)
{
    return Math.Ceiling(myDate.Month / 3);
}
DateTime GetQuarterStartingDate(DateTime myDate)
{
    return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
}


要基于四月份计算季度 ,可以使用.NET的时间段库:
To calculate the quarter based on the month April, you can use the Time Period Library for .NET:
// ----------------------------------------------------------------------
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的时间段库 [



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);


这篇关于季度日期的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆