C# 从给定日期计算季度的开始日期和名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492079/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:04:27  来源:igfitidea点击:

Calculate the start-date and name of a quarter from a given date

c#date

提问by cllpse

How can I find the start-date and name (1, 2, 3, etc.) of a quarter from a given date?

如何从给定日期找到季度的开始日期和名称(1、2、3 等)?

采纳答案by Joe

Something like (untested):

类似的东西(未经测试):

DateTime date;
int quarterNumber = (date.Month-1)/3+1;
DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber-1)*3+1,1);
DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);

回答by ZombieSheep

int GetQuarterName(DateTime myDate)
{
    return (int)Math.Ceiling(myDate.Month / 3.0);
}

DateTime GetQuarterStartingDate(DateTime myDate)
{
    return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
}

GetQuarterNamegets the "next" integer value of the current month number / 3.

GetQuarterName获取当前月份数的“下一个”整数值 / 3。

GetQuarterStartingDateuses the output from GetQuarterNameto work out the month value, the year part of the original date, and 1 to represent the first day of the month to return.

GetQuarterStartingDate使用 from 的输出计算GetQuarterName出月份值、原始日期的年份部分,以及 1 表示要返回的月份的第一天。

(Apologies for making no sense, I have flu. :( )

(抱歉毫无意义,我得了流感。:()

回答by anonymous

https://msdn.microsoft.com/en-us/library/ms127415(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms127415(v=vs.110).aspx

using Microsoft.VisualBasic;
var quarter = DateAndTime.DatePart(DateInterval.Quarter, (DateTime)dateTimePickerDateTime.Value);

回答by youzer

        var date = new DateTime(2015, 3, 15);

        var quarter = (date.Month + 2) / 3;

        var quarterStartMonth = 3 * quarter - 2;

        var quarterStartDate = new DateTime(date.Year, quarterStartMonth, 1);

回答by super cool

A simpler two liner with live demo here+ press F8to run .

一个较为简单的内胆采用现场演示这里+按F8来运行。

var date = DateTime.Now; //Give you own DateTime
int offset = 2, monthsInQtr = 3;

var quarter = (date.Month + offset) / monthsInQtr; //To find which quarter 
var totalMonths = quarter * monthsInQtr;

var startDateInQtr = new DateTime(date.Year, totalMonths - offset, 1); //start date in quarter 

If you are looking at last dayof the quarter use DateTime.DaysInMonth

如果您正在查看本季度的最后一天,请使用DateTime.DaysInMonth

var endDateInQtr = new DateTime(date.Year, totalMonths, DateTime.DaysInMonth(date.Year, totalMonths));

回答by Ignas

I think this solution would work pretty well. It takes up more line, bus is very verbose! `

我认为这个解决方案会很好用。占线比较多,巴士很啰嗦!`

private DateTime GetFirstDayOfYearlyQuarter(DateTime value)
{
    switch (value.Month)
    {
        case 1:
        case 2:
        case 3:
            return new DateTime(value.Year, 1, 1);
        case 4:
        case 5:
        case 6:
            return new DateTime(value.Year, 4, 1);
        case 7:
        case 8:
        case 9:
            return new DateTime(value.Year, 7, 1);
        case 10:
        case 11:
        case 12:
            return new DateTime(value.Year, 10, 1);
        default:
            throw new Exception(@"ˉ\_(ツ)_/ˉ");
    }
}

`

`

P.S. This only finds first day of quarter, but you can easily extend this to find number of quarter, etc.

PS 这只能找到季度的第一天,但​​您可以轻松扩展它以查找季度数等。

回答by Verard Sloggett

    public static class DateTimeExtensions
{
    public static DateTime StartOfQuarter(this DateTime _date)
    {
        var quarter = decimal.ToInt16(Math.Ceiling(_date.Month / 3.0m));
        return new DateTime(_date.Year, quarter, 1);
    }
}

use

var quarterStart = DateTime.Today.StartOfQuarter();