English 中文(简体)
如何根据目前的日期发现财政年度?
原标题:How to discover financial Year based on current datetime?

我需要一个基于目前或今天时间的财政年度。

如果我们今天审议这个日期是10 April 2011,则需要产出为2012年财务年<<>代码>,在某些情况下,需要以短格式显示与相同的产出。 这两种方式都需要显示。

按照我们的要求,考虑的财政年度为:April (the present year)至March>(following year)。

根据目前的日期......产出的假设取决于目前时间在下文所述期间或期限。

From 01April2011 to 31March2012 - Financial Year 2012 or FY2012
From 01April2012 to 31March2013 -  Financial Year 2013 or FY2013
From 01April2013 to 31March2014 -  Financial Year 2014 or FY2014
.
.
.

页: 1

另一个例子: 如果我们今天的会期为2012年4月16日,那么产出就需作为<条码>2013年财务年<>和<条码>。

Please help how to acheive the same in very short format using LINQ or Regex in C#, .Net3.5

最佳回答

A couple of Extension methods

public static class DateTimeExtensions
{
    public static string ToFinancialYear(this DateTime dateTime)
    {
        return "Financial Year " + (dateTime.Month >= 4 ? dateTime.Year + 1 : dateTime.Year);
    }

    public static string ToFinancialYearShort(this DateTime dateTime)
    {
        return "FY" + (dateTime.Month >= 4 ? dateTime.AddYears(1).ToString("yy") : dateTime.ToString("yy"));
    }
}
问题回答

此前,我通过创建<代码>Financial Year类别:

public class FinancialYear
{
    int yearNumber;
    private static readonly int firstMonthInYear = 4;

    public static FinancialYear Current
    {
        get { return new FinancialYear(DateTime.Today); }
    }

    public FinancialYear(DateTime forDate) 
    {
         if (forDate.Month < firstMonthInYear) {
             yearNumber = forDate.Year + 1;
         }
         else {
             yearNumber = forDate.Year;
         }
    }

    public override string ToString() {
        return yearNumber.ToString();
    }
}

Other points:

  • Have a look at IFormatProvider to see how you can customize formatting (you could provide an overload of ToString that takes a format argument like DateTime does.
  • override Equals, and implement IEquitable to provide equality.
  • Implement IComparable to give comarisons.
  • You could also implement your own == < > >= and <= operators for the class.

如果该财政年度是可变的(例如,一个公司财政年度可能运行7月,6月,而不是4月之后;3月征税年)。

/// <summary>
/// Extension method to get the start of the financial year
/// </summary>    
public static DateTime GetStartOfFinancialYear(this DateTime date, int startMonthOfFinancialYear)
{
    if (startMonthOfFinancialYear < 1 || startMonthOfFinancialYear > 12)
        throw new ArgumentException("Must be between 1 and 12","startMonthOfFinancialYear");

    DateTime rtn = new DateTime(date.Year,startMonthOfFinancialYear,1);
    if (date.Month < startMonthOfFinancialYear)
    {
        // Current FY starts last year - e.g. given April to March FY then 1st Feb 2013 FY starts 1st April 20*12*
        rtn = rtn.AddYears(-1);
    }

    return rtn;
}

// Example, Financial Year starts in July
DateTime startFY = DateTime.Now.GetStartOfFinancialYear(7);
DateTime endFY = startFY.AddYears(1).AddDays(-1);

To improve on Russ answer above, I would suggest:

  • consolidate 2 extension methods into 1 - common addition logic
  • add arguments for Short/Long and the month (some need Oct instead of April)
  • add default values
  • leave out the "Financial Year" because some might use "Fiscal Year"

public static string ToFYString(this DateTime dateTime, bool longFlag = false, int monthLimit = 3)
{
   var format = longFlag ? "yyyy" : "yy";
   return (dateTime.Month > monthLimit ? dateTime.AddYears(1).ToString(format) : dateTime.ToString(format));
}
  1. if april is your new FY, and you want short, use .ToFYString()
  2. if october is your new FY and you want short use .ToFYString(monthLimit: 9)
  3. if april is your new FY, and you want long, use .ToFYString(true)
  4. if october is your new FY, and you want long, use .ToFYString(true, 9)

• 跟踪代码,以获取财政年度或期间值。

public int GetFinancialYear(DateTime date)
{
    return date.Month > 6 ? date.Year + 1 : date.Year;
}

public int GetFinancialPeriod(DateTime date)
{
    return date.Month > 6 ? date.Month - 6 : date.Month + 6;
}

我不敢肯定,为什么你们需要L.Q,但获得这样的东西就够了。

DateTime today = DateTime.Today;
if(today.Month <= 3)
       make this string "Financial Year " + today.ToString("yy")); // OR yyyy for 2013
else 
       "Financial Year " + today.AddYears(1).ToString("yy"));
public class FinancialYear
{
    public YearRepresentation ResolveFinancialYear(DateTime currentDate)
    {
        YearRepresentation financialYear = new YearRepresentation();
        int year = (currentDate.Month >= 4) ? currentDate.AddYears(1).Year : currentDate.Year;
        financialYear.SetYear(year);

        return financialYear;
    }
}

public class YearRepresentation
{
    public string LongYear { get; set; }

    public string ShortYear { get; set; }

    public void SetYear(int year)
    {
        this.LongYear = "Financial Year " + year;
        this.ShortYear = "FY " + year;
    }
}

这是目前财政年度的一个例子。

        string FinYear=null;

        if (DateTime.Today.Month > 3)
        {

            FinYear = "1/4/" + DateTime.Today.Year;
        }

        else
        {
            FinYear = "1/4/" + (DateTime.Today.Year - 1);
        }

Here used Valued Tuple (Finacial year start from July so, I used 7. If you want

年限至年限

然后,格式才需要使用月度和年期财产。

public  static (DateTime, DateTime) GetCurrentFinacialYearDateRange()
    {
        if(DateTime.Now.Month >= 7)
        {
            DateTime startDate = new DateTime(DateTime.Today.Year, 7, 1); // 1st July this year
            DateTime endDate = new DateTime(DateTime.Today.Year + 1, 7, 1).AddDays(-1); // Last day in June next year
            return (startDate, endDate);
        }
        else
        {
            DateTime startDate = new DateTime(DateTime.Today.Year-1, 7, 1); // 1st July this year
            DateTime endDate = new DateTime(DateTime.Today.Year, 7, 1).AddDays(-1); // Last day in June next year
            return (startDate, endDate);
        }
        
    }




相关问题
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!