Click here to Skip to main content
15,567,447 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to calculate Quarter month and half year from given date.
I did this for quarter month.but I think its wrong.pls help me.

What I have tried:

private void dtDate_ValueChanged(object sender, EventArgs e)
{
DateTime date=dtDate.Value;


dtQuarter.Value = (dtDate.Value.AddMonths(3));



}
Posted
Updated 29-Mar-22 23:12pm
Comments
Ralf Meier 4-Aug-16 3:34am    
Which should be the result for quarter-month ?
January to March = Quarter 1
April to June = Quarter 2
July to September = Quarter 3
October to December = Quarter 4

... and Half Year ?
January to June = 1st
July to December = 2nd
Member 12385326 4-Aug-16 4:01am    
it should be in date only.i mean if I select start date=2013-07-01 then end date=2013-09-30 like this.

1 solution

For quarter, try:
C#
public DateTime GetQuarterEnd(DateTime dt)
    {
    int month = dt.Month;
    month = ((month + 2) / 3) * 3;
    return new DateTime(dt.Year, month, 1).AddMonths(1).AddDays(-1);
    }
You add one month then subtract one day to get the last day of the quarter.
 
Share this answer
 
Comments
Member 12385326 4-Aug-16 4:20am    
@OriginalGriff please suggest how to set this value for datetimepicker.
I have 2 datetimpicker. o the valueChanged Eent of fisrt second displays the quarter date
OriginalGriff 4-Aug-16 4:31am    
Ummm...

myOutputDateTimePicker.Value = GetQuarterEnd(myInputDateTimePicker.Value);
Member 12385326 4-Aug-16 4:34am    
Thanks a lot. it works
Member 12385326 4-Aug-16 4:35am    
please provide solution for half year also
OriginalGriff 4-Aug-16 4:43am    
Oh come on!
That one is either a small change to the existing code or a trivial "if" test!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900