有谁知道如何在.NET中构建DateTime作为当前季度的第一天?
答案 0 :(得分:6)
public DateTime GetCurrentQuarter(DateTime date)
{
int startingMonthOfQuarter = (((date.Month - 1) / 3) * 3) + 1;
return new DateTime(date.Year, startingMonthOfQuarter, 1);
}
答案 1 :(得分:3)
var currentTime = DateTime.Now;
var year = currentTime.Year;
DateTime[] quarterStarts = {new DateTime(year, 1, 1), new DateTime(year, 4, 1), new DateTime(year, 7, 1), new DateTime(year, 10, 1)};
var currentStart = quarterStarts.Where(s => s < currentTime).Last();
答案 2 :(得分:1)
@yogesh是对的,这是我在sql sp中使用的内容:
CREATE FUNCTION [dbo].[fn_startOfQtr](@dt DATETIME)
RETURNS DATETIME AS
BEGIN
declare @ret DATETIME
declare @qtr int
set @qtr = ((month(@dt)-1)/3)+1
set @ret = cast(str(@qtr * 3 - 2) + '/1/' + str(year(@dt)) as DATETIME)
return @ret
END