English 中文(简体)
日期之间的季度数
原标题:Count of quarter between dates

我想计算特定时间段的总数(一年)。

例如:

start date = 1-june -2009
end date = 18-july-2011

count should be = 10.

one more 
start date = 4-Jan-2009 
end date = 27-oct -2010
count =8.

我未能取得正确结果。

最佳回答

您的例子是错误的:4-Jan-200927-oct - 2010<>/code>之间只有7/4。

你可以简单地提及<代码>。 页: 1

VB:

Public Shared Function getQuartersBetween(ByVal d1 As Date, ByVal d2 As Date) As Int32
    Return DateDiff(DateInterval.Quarter, d1, d2)
End Function

C#:

public static int getQuartersBetween(System.DateTime d1, System.DateTime d2)
{
    return Microsoft.VisualBasic.DateAndTime.DateDiff(DateInterval.Quarter, d1, d2);
}

或者你可以写一下自己的执行情况:

public class Quarter
{

    public static long GetQuarters(DateTime dt1, DateTime dt2) 
    { 
        double d1Quarter = GetQuarter(dt1.Month); 
        double d2Quarter = GetQuarter(dt2.Month); 
        double d1 = d2Quarter - d1Quarter; 
        double d2 = (4 * (dt2.Year - dt1.Year)); 
        return Round(d1 + d2); 
    } 

    private static int GetQuarter(int nMonth) 
    { 
        if (nMonth <= 3) 
            return 1; 
        if (nMonth <= 6) 
            return 2; 
        if (nMonth <= 9) 
            return 3; 
        return 4; 
    } 

    private static long Round(double dVal) 
    { 
        if (dVal >= 0) 
              return (long)Math.Floor(dVal); 
        return (long)Math.Ceiling(dVal); 
    } 
}

or in VB.NET:

Public Class Quarter

    Public Shared Function GetQuarters(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Long
        Dim d1Quarter As Double = GetQuarter(dt1.Month)
        Dim d2Quarter As Double = GetQuarter(dt2.Month)
        Dim d1 As Double = d2Quarter - d1Quarter
        Dim d2 As Double = (4 * (dt2.Year - dt1.Year))
        Return Round(d1 + d2)
    End Function

    Private Shared Function GetQuarter(ByVal nMonth As Integer) As Integer
        If nMonth <= 3 Then
            Return 1
        End If
        If nMonth <= 6 Then
            Return 2
        End If
        If nMonth <= 9 Then
            Return 3
        End If
        Return 4
    End Function

    Private Shared Function Round(ByVal dVal As Double) As Long
        If dVal >= 0 Then
            Return CLng(Math.Floor(dVal))
        End If
        Return CLng(Math.Ceiling(dVal))
    End Function

End Class
问题回答

阁下: 低于代码

 public static void Main()
    {
        //Application.Run(new XmlTreeDisplay());
        int monthdiuff = monthDifference(Convert.ToDateTime("01/04/09"), Convert.ToDateTime("10/27/10"));
        Console.WriteLine(monthdiuff);
        int totalQuater = (monthdiuff / 3) + (monthdiuff%3);
        Console.WriteLine(totalQuater);
        Console.ReadLine();
    }

    private static int monthDifference(DateTime startDate, DateTime endDate)
    {
        int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
        return Math.Abs(monthsApart);
    }

如果没有一些法典来审视我,就能够帮助你找到你的具体问题。

If it were me I would probably find the difference between the dates in days, then divide by number of days in a quarter (91 or so). I m sure that C# has some kind of date parsing module that can read in the dates as a string, giving you two objects that you could then subtract to find the difference in days.

这是一种粗略的计算方式,根据您的假设,你可以选择修改,因为这样做非常好。

DateTime dt1 = new DateTime(2009, 1, 1);// new DateTime(2009, 6, 1);
DateTime dt2 = new DateTime(2010, 10, 27);// new DateTime(2011, 7, 18);
if (dt1.Month < 4)
  dt1 = new DateTime(dt1.Year,1,1);
else if (dt1.Month < 7)
  dt1 = new DateTime(dt1.Year,4,1);
else if (dt1.Month < 10)
  dt1 = new DateTime(dt1.Year,7,1);
else
  dt1 = new DateTime(dt1.Year,10,1);
if (dt2.Month < 4)
   dt2 = new DateTime(dt2.Year, 3, DateTime.DaysInMonth(dt2.Year, 3)); 
else if (dt2.Month < 7)
   dt2 = new DateTime(dt2.Year, 6, DateTime.DaysInMonth(dt2.Year, 6));
else if (dt2.Month < 10)
   dt2 = new DateTime(dt2.Year, 9, DateTime.DaysInMonth(dt2.Year, 9));
else
   dt2 = new DateTime(dt2.Year, 12, DateTime.DaysInMonth(dt2.Year, 12));

TimeSpan ts = dt2 - dt1;
int quarters = (int) ts.TotalDays/90;
Console.WriteLine(quarters);

我将日期按你的意愿排列,然后假设90天,把iff变成 in。 您提到的例子,如果适合你的话,就够了。

If the definition of a quarter is a 90-day difference, it s easy of course:

    internal static int GetNumberOfQuarters(DateTime p_DtStart, DateTime p_DtEnd)
    {
        TimeSpan span = p_DtEnd.Subtract(p_DtStart);
        return (int)span.TotalDays % 90;
    }

但是,这并不是你所回顾的。 与此有关的内容(没有经过测试,但你听了想法)

internal static class DateTimeTools
{
    internal static int GetNumberOfQuartersBetweenDates(DateTime startDate, DateTime endDate)
    {
        int iYearStart, iYearEnd, iMonthStart, iMonthEnd, iDayStart, iDayEnd;
        iYearStart = startDate.Year;
        iYearEnd = endDate.Year;
        iMonthStart = startDate.Month;
        iMonthEnd = endDate.Month;
        iDayStart = startDate.Day;
        iDayEnd = endDate.Day;

        int iYearDiff, iQuarterDiff, iDayDiff;
        iYearDiff = iYearEnd - iYearStart;
        iQuarterDiff = iMonthEnd % 3 - iMonthStart % 3;
        iDayDiff = iDayEnd - iDayStart;

        int iNumOfQuarters = 0;

        // at least a year difference? 
        if ((iYearDiff > 0 && iQuarterDiff > 0) || iYearDiff > 0 && iQuarterDiff == 0 && iDayDiff >= 0)
        {
            iNumOfQuarters = iYearDiff * 4 + iQuarterDiff;
        }
        // at least a quarter difference?
        // within different years
        if ((iYearDiff > 0 && iQuarterDiff <= 0)) // eg, dec 2010 - feb 2011 iYearDiff 1 iQuarterDiff -3
        {
            if ((iQuarterDiff == -3 && iDayDiff >= 0) || iQuarterDiff > -3)
            {
                iNumOfQuarters = iQuarterDiff + 4;
            }
        }
        // within the same year
        if (iYearDiff == 0 && iQuarterDiff > 0)
        {
            if ((iQuarterDiff == 1 && iDayDiff >= 0) || iQuarterDiff > 1)
            {
                iNumOfQuarters = iQuarterDiff;
            }
        }
        return iNumOfQuarters;
    }
}

Regards, Nico

public static string GetQuarter(this DateTime date)
        {
            var quarterList = new List<string>();

            if (date.Month >= 1 && date.Month <= 3)
                return "Q1";
            else if (date.Month >= 4 && date.Month <= 6)
                return "Q1,Q2";
            else if (date.Month >= 7 && date.Month <= 9)
                return "Q1,Q2,Q3";
            else
                return "Q1,Q2,Q3,Q4";
        }

如果你期望收到一份季度清单,那么也可以作为延伸方法加以利用。

2. 促成季度差异的简单公式:

{
    int firstQuarter = getQuarter(first);
    int secondQuarter = getQuarter(second);
    return 1 + Math.Abs(firstQuarter - secondQuarter);
}

private static int getQuarter(DateTime date)
{
    return (date.Year * 4) + ((date.Month - 1) / 3);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!