找到当前季度的功能 [英] Function to find Current Quarter of the Year
本文介绍了找到当前季度的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以找到一个C#代码,通过它我可以找到当年的季度。
例如:1月,2月,3月跌入第1季度
4月-JUne:第2季度
7月 - 9月:第3季度
10月 - 12月 - 季度4
解决方案
如果您知道答案的原因,您发布了一个问题?
无论如何,我认为这个方法将很好地实现为这样的扩展方法
静态 类 ExtensionMethods
{
public static int GetQuarter( this DateTime dt)
{
< span class =code-keyword> return (dt.Month - 1 )/ 3 + 1 跨度>;
}
}
并且调用如下:
int iQuarter = DateTime.Now.GetQuarter();
以下几行为您提供了技巧。根据您的需要进行修改
int mth = DateTime.Now 。月;
if (mth < = 3 )
{
Console.WriteLine( Quarter 1) ;
}
else if (mth > 3 && mth < = 6 )
{
Console.WriteLine( 第2季跨度>);
}
else if (mth > 6 && mth < = 9 )
{
Console.WriteLine( 第3季跨度>);
}
else if (mth > 9 && mth < = 12 )
{
Console.WriteLine( 第4季度跨度>);
}
希望这能帮到你
.NET时间段库包含enum YearQuarter
和类季度
:
/ / ----------------------------------- -----------------------------------
public void YearQuartersSample()
{
Year year = new 年( 2012 );
ITimePeriodCollection quarters = year.GetQuarters();
Console.WriteLine( 年度季度:{0},年份);
// >年度季度:2012年; 01.01.2012 - 31.12.2012 | 365.23:59
foreach (季度 季度)
{
Console.WriteLine( Quarter:{0},quarter);
}
// >季度:2012年第一季度; 01.01.2012 - 31.03.2012 | 90.23:59
// >季度:2012年第二季度; 01.04.2012 - 30.06.2012 | 90.23:59
// >季度:2012年第3季度; 01.07.2012 - 30.09.2012 | 91.23:59
// >季度:2012年第四季度; 01.10.2012 - 31.12.2012 | 91.23:59
} // YearQuartersSample
Hi,
Can anybody pls find for me a C# code through which i can find what quarter it is for the current year .
eg: Jan,Feb,Mar Falls in Quarter1
Apr-JUne: Quarter2
July-Sept:Quarter3
Oct-Dec-Quarter4
解决方案
If you know the answer why you post a question?
Anyway, I think this method would be nice implemented as an extension method like this
static class ExtensionMethods { public static int GetQuarter(this DateTime dt) { return (dt.Month - 1) / 3 + 1; } }
and called like:
int iQuarter = DateTime.Now.GetQuarter();
Below are few lines which does the trick for you. Modify it as per your need
int mth = DateTime.Now.Month; if (mth <= 3) { Console.WriteLine("Quarter 1"); } else if (mth > 3 && mth <= 6) { Console.WriteLine("Quarter 2"); } else if (mth > 6 && mth <= 9) { Console.WriteLine("Quarter 3"); } else if (mth > 9 && mth <= 12) { Console.WriteLine("Quarter 4"); }
Hope this will help you out
The Time Period Library for .NET includes theenum YearQuarter
and theclass Quarter
:
// ---------------------------------------------------------------------- public void YearQuartersSample() { Year year = new Year( 2012 ); ITimePeriodCollection quarters = year.GetQuarters(); Console.WriteLine( "Quarters of Year: {0}", year ); // > Quarters of Year: 2012; 01.01.2012 - 31.12.2012 | 365.23:59 foreach ( Quarter quarter in quarters ) { Console.WriteLine( "Quarter: {0}", quarter ); } // > Quarter: Q1 2012; 01.01.2012 - 31.03.2012 | 90.23:59 // > Quarter: Q2 2012; 01.04.2012 - 30.06.2012 | 90.23:59 // > Quarter: Q3 2012; 01.07.2012 - 30.09.2012 | 91.23:59 // > Quarter: Q4 2012; 01.10.2012 - 31.12.2012 | 91.23:59 } // YearQuartersSample
这篇关于找到当前季度的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文