How to open a fiscal year based on the current time?

I need a fiscal year based on the current or today's datetime.

Suppose if we think that date is date 10 April 2011, then I need outputs like Financial Year 2012, and in some cases I need to display the same result in short format as FY12. Both ways I want to display.

In our request, a fiscal year counts from April(current year) to March(next year).

Based on the current time and time, the output script depends on the current time and time, which should be below the specified period or duration.

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
.
.
.

etc....

Another example: if we accept today datetime as 16April2012, then the output is needed as Financial Year 2013, as well FY13.

, , , LINQ Regex #,.Net3.5

+5
7

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"));
    }
}
+10

, FinancialYear:

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

:

  • IFormatProvider, , ( ToString, , DateTime.
  • Equals IEquitable .
  • IComparable, .
  • == < → = <= .
+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);
+1

, :

  • 2 1 -
  • Short/Long ( )
  • " ", " "

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));
}
  • april - FY, , .ToFYString()
  • - FY, .ToFYString(monthLimit: 9)
  • april - FY, , .ToFYString(true)
  • - FY, , .ToFYString(true, 9)
+1

I'm not sure why you need LINQ, but that won't be enough.

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"));
0
source
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;
    }
}
0
source

This is an example of a search for the current fiscal year. I am submitting this answer for a Google search.

        string FinYear=null;

        if (DateTime.Today.Month > 3)
        {

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

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

All Articles