我需要获得给定日期的通讯三个月(3个月,即第一个,第二个月和第三个月的第一个三个月)。使用c#System.DateTime结构我没有设法找到我正在寻找的方法。所以我这样解决了:
DateTime sDeathDate = DateTime.Parse("18/09/1970");
int iDeathTrimester = Convert.ToInt32(Math.Round(Convert.ToDouble(sDeathDate.Month) / 3 + 0.25));
如果有人知道更简单的方法,请回答。
安德烈
答案 0 :(得分:12)
假设1月 - 3月是三个月,4月 - 6月是妊娠2月,7月 - 9月是3个月,10月 - 12月是妊娠4个月,那么你可以使用
int trimester = (sDeathDate.Month - 1) / 3 + 1
这与四分之一相同,你的意思是不同吗?
答案 1 :(得分:3)
Math.Ceiling返回大于或等于指定数字的最小整数。
DateTime sDeathDate = DateTime.Parse("18/11/1970");
int trimester = (int)Math.Ceiling(sDeathDate.Month/3.0);
请注意,代码使用 3.0 执行浮点除法而不是整数除法
答案 2 :(得分:1)
Time Period Library for .NET包括季度类(3个月的期限):
// ----------------------------------------------------------------------
public void QuarterOfDateSample()
{
DateTime moment = new DateTime( 1970, 9, 15 );
Console.WriteLine( "Quarter : {0}", new Quarter( moment ) );
// > Quarter : Q3 1970; 01.07.1970- 30.09.1970| 91.23:59
Console.WriteLine( "Quarter : {0}", new Quarter( 2006, YearQuarter.First ) );
// > Quarter : Q1 2006; 01.01.2006 - 31.03.2006 | 89.23:59
} // QuarterOfDateSample
答案 3 :(得分:0)
如果您使用Math.Round (Double, MidpointRounding),则可以在不添加0,25
见到你