C# 如何根据当前日期时间发现财政年度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8878399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to discover financial Year based on current datetime?
提问by venkat
I need a financial year based on current or today's datetime.
我需要一个基于当前或今天日期时间的财政年度。
Suppose if we consider today's date is 10 April 2011, then i need the outputs as Financial Year 2012and for some cases i need to display the same output in short format as FY12. Both ways i want to display.
假设如果我们考虑今天的日期是10 April 2011,那么我需要输出 asFinancial Year 2012并且在某些情况下我需要以短格式显示相同的输出FY12。两种方式我都想显示。
In our requirement financial Year considered is from April(the current year) to March(following year).
在我们的要求中,考虑的财政年度是从April(当年)到March(下一年)。
Based on current datetime...the scenario of output is depends on the current datetime falls in the below said 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
.
.
.
so on....
很快....
Another example: If we take today's datetime as 16April2012, then the output is needed as Financial Year 2013and also FY13.
另一个例子:如果我们将今天的日期时间取为 16April2012,则需要输出为 Financial Year 2013和FY13。
Please help how to acheive the same in very short format using LINQ or Regex in C#, .Net3.5
请帮助如何在 C#、.Net3.5 中使用 LINQ 或 Regex 以非常短的格式实现相同的功能
采纳答案by Russ Cam
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"));
}
}
回答by Andy Skirrow
I've done the before by creating a FinancialYearclass:
我通过创建一个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();
}
}
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.
- 查看 IFormatProvider 以了解如何自定义格式(您可以提供像 DateTime 那样采用格式参数的 ToString 重载。
- 覆盖 Equals,并实现 IEquitable 以提供相等性。
- 实现 IComparable 以给出comarisons。
- 您还可以为该类实现自己的 == < > >= 和 <= 运算符。
回答by V4Vendetta
I am not sure why you would need LINQ but won't something like this suffice
我不确定你为什么需要 LINQ 但这样的东西还不够
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"));
回答by Sabry
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;
}
}
回答by Ryan
Where the financial year is variable (e.g. a company financial year may run July > June rather than follow April > March tax year)
财政年度是可变的(例如,公司财政年度可能是七月 > 六月,而不是四月 > 三月的纳税年度)
/// <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);
回答by Taterhead
To improve on Russ' answer above, I would suggest:
为了改进上面 Russ 的回答,我建议:
- 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"
- 将 2 种扩展方法合并为 1 - 通用加法逻辑
- 添加短/长和月份的参数(有些需要十月而不是四月)
- 添加默认值
- 省略“财政年度”,因为有些人可能会使用“财政年度”
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));
}
- if april is your new FY, and you want short, use
.ToFYString() - if october is your new FY and you want short use
.ToFYString(monthLimit: 9) - if april is your new FY, and you want long, use
.ToFYString(true) - if october is your new FY, and you want long, use
.ToFYString(true, 9)
- 如果四月是你的新财年,而你想要短,使用
.ToFYString() - 如果 10 月是你的新财年并且你想要短期使用
.ToFYString(monthLimit: 9) - 如果四月是你的新财年,你想要很长,使用
.ToFYString(true) - 如果 10 月是你的新财年,而你想要很长,请使用
.ToFYString(true, 9)
回答by Bino Kochumol Varghese
This an example for finding current financial year.
这是查找当前财政年度的示例。
string FinYear=null;
if (DateTime.Today.Month > 3)
{
FinYear = "1/4/" + DateTime.Today.Year;
}
else
{
FinYear = "1/4/" + (DateTime.Today.Year - 1);
}

