2011-06-24 73 views
2

我們的財政年度開始於每年4月1日。因此,本財政年度是2011年4月1日。工作開始的財政年度

無論當前日期如何,我該如何得到這個日期?

例如,今天是2011-06-24,我該如何返回2011-04-30。

如果今天是2012-02-05,我仍然需要它返回2011-04-30。但是,如果今天是2012-07-06,則應該返回2012-04-30。

因此,基本上,財務日期的年份不應該改變到當年,直到5月1日爲止。

即一些例子

2011-03-05 = 2010-04-30 
2011-04-06 = 2011-04-30 
2010-01-15 = 2009-04-30 
2015-09-01 = 2015-04-30 
2020-12-25 = 2020-04-30 
2021-02-26 = 2020-04-30 
+0

只需檢查當前日期,如果它不到4月1日,那麼該年將是上一年,否則該年將是當年。 – Mudassir

+0

編輯我的答案,當你編輯一些數據的問題,只是使用我寫的方法(我希望在VB是C#的確切翻譯...我是一個C#研究員) – balexandre

+0

請你解釋你爲什麼要求4月1日和你的例子都會在4月30日迴歸?財政年度第一個歷月結束的意義何在? –

回答

1

使用:

Dim dateTime__1 As New DateTime(DateTime.Now.AddMonths(-4).Year, 4, 30) 
+0

它總是最好把它包裝在一個共享/靜態方法,所以它可以隨時調用,而不必每次都提及所有的東西:) – balexandre

+0

如何在啓動時設置一次,然後調用'dateTime__1'它是什麼需要? – oshirowanen

4

沒有內置的功能,但它很容易地構建自己:

  • 獲取當前日期
  • 如果是4月及以後,使用年和設置月和日至4月1日
  • 如果是在四月之前,請使用前一年的4月1日。
+0

+1。正是我想到的。 – Mudassir

2

你可以簡單的用一個靜態方法:

在VB
public static GetStartOfFinancialYear() { 

    DateTime startOfYear = new DateTime(Datetime.UtcNow.Year, 4, 30); 
    return 
     DateTime.UtcNow < startOfYear ? 
      startOfYear.AddYears(-1) : startOfYear; 

} 

public shared Function GetStartOfFinancialYear() As DateTime 

    Dim startOfYear As New DateTime(DateTime.Now.Year, 4, 30) 

    If DateTime.UtcNow < startOfYear Then 
     return startOfYear.AddYears(-1) 
    Else 
     return startOfYear 
    End If 

End Function 
+0

我打算以同樣的方式回答,但這不是問什麼 - 2012年3月12日它應該返回4/1/2011和2012年5月12日它應該返回2012年4月1日 –

+0

我沒有正確的問題,他在我回答時編輯它。 – balexandre

+0

謝謝,我犯了一個錯誤,應該是30而不是1,但是我可以看到我只需要將示例中的1改爲30。 – oshirowanen

0

Time Period Library for .NET包括類年與支持的財政時間段秒。

您可以定義具有自定義年份基準月的財務時間日曆。下面的示例使用月作爲財年開端:

// ------------------------------------------------------------------------ 
public class FiscalTimeCalendar : TimeCalendar 
{ 
    // ---------------------------------------------------------------------- 
    public FiscalTimeCalendar() : 
    base(new TimeCalendarConfig 
     { 
     YearBaseMonth = YearMonth.October, // October year base month 
     YearWeekType = YearWeekType.Iso8601, // ISO 8601 week numbering 
     YearType = YearType.FiscalYear// treat years as fiscal years 
     }) 
    { 
    } // FiscalTimeCalendar 
} // class FiscalTimeCalendar 

這就是用法: 收起

// ---------------------------------------------------------------------- 
public void FiscalYearSample() 
{ 
    FiscalTimeCalendar calendar = new FiscalTimeCalendar(); // use fiscal periods 

    DateTime moment1 = new DateTime(2006, 9, 30); 
    Console.WriteLine("Fiscal Year of {0}: {1}", moment1.ToShortDateString(), 
        new Year(moment1, calendar).YearName); 
    // > Fiscal Year of 30.09.2006: FY2005 
    Console.WriteLine("Fiscal Quarter of {0}: {1}", moment1.ToShortDateString(), 
        new Quarter(moment1, calendar).QuarterOfYearName); 
    // > Fiscal Quarter of 30.09.2006: FQ4 2005 

    DateTime moment2 = new DateTime(2006, 10, 1); 
    Console.WriteLine("Fiscal Year of {0}: {1}", moment2.ToShortDateString(), 
        new Year(moment2, calendar).YearName); 
    // > Fiscal Year of 01.10.2006: FY2006 
    Console.WriteLine("Fiscal Quarter of {0}: {1}", moment1.ToShortDateString(), 
        new Quarter(moment2, calendar).QuarterOfYearName); 
    // > Fiscal Quarter of 30.09.2006: FQ1 2006 
} // FiscalYearSample 

該庫還包括類Halfyear季度與支持財政時間段。

相關問題