简体   繁体   English

在c#中,如何获得下一个季度的最后一天给出日期。?

[英]in c #, how to get the last day of next quarter giving the date.?

In c#, how to get the last day of next quarter giving the date?在c#中,如何获得下一个季度的最后一天给出日期?

For example, given date is 2014-12-02, I need to return the date 2015-03-31.例如,给定日期是 2014-12-02,我需要返回日期 2015-03-31。

Very simple:很简单:

var given = new DateTime(2014, 02, 12);

var result =
    given.Date
        .AddDays(1 - given.Day)
        .AddMonths(3 - (given.Month - 1) % 3)
        .AddDays(-1);

//2014-03-31

If I input 2014-08-12 I get 2014-09-30 .如果我输入2014-08-12我得到2014-09-30

Here are the results for the start of each month for the year:以下是该年度每个月初的结果:

结果

Is that what you wanted?那是你想要的吗?

Be carefull with this code!小心这个代码! For example date 31.01.2018 will return 30.03.2018.例如日期 31.01.2018 将返回 30.03.2018。 This will work as expected:这将按预期工作:

var given = new DateTime(2018, 01, 31);    
var result =
        given.Date
            .AddMonths(3 - (given.Month - 1) % 3);
    result = result.AddDays(-result.Day);
//2018-03-31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM