Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Setting DateTimePicker to last quarter

Posted on 2015-01-19
Medium Priority
316 Views
I have a datetime picker that I am trying to set to "Last Quarter" currently it shows 1/1/2015 - 3-31-2015
not last quarter 10-1-2014 - 12-31-2014

``````                        DateTime date = DateTime.Now;
int Quarter = (date.Month - 1) / 4 + 1;
DateTime first = new DateTime(date.Year, 3 * Quarter - 2, 1);
dateTimePicker_ReportsStart.Text = first.ToString("MM,dd,yyy 00:00:00");
dateTimePicker_ReportsTimeStart.Text = first.ToString("MM,dd,yyy 00:00:00");
dateTimePicker_ReportsEnd.Text = last.ToString("MM,dd,yyy 23:59:59");
dateTimePicker_ReportsTimeEnd.Text = last.ToString("MM,dd,yyy 23:59:59");
``````
0
Question by:r3nder
• 2
• 2

LVL 45

Expert Comment

ID: 40558246
>>int Quarter = (date.Month - 1) / 4 + 1;
Is this what you want:
int Quarter = (date.Month - 3) / 4 + 1;
as there are 3 months in a quarter
0

LVL 27

Accepted Solution

Shaun Kline earned 2000 total points
ID: 40558250
You appear to be calculating the quarter for today.
Consider changing
``````DateTime first = new DateTime(date.Year, 3 * Quarter - 2, 1);
``````
to
``````DateTime first = new DateTime(date.Year, 3 * Quarter - 2, 1).AddMonth(-3);
``````
to get the prior quarter start date.
0

LVL 6

Author Comment

ID: 40558278
@Shaun
that fixes first day but not last
First is 10-1-2014 but last is 3-31-2015 and if I add the same logic to last-- I get 9-30-2014
0

LVL 27

Assisted Solution

Shaun Kline earned 2000 total points
ID: 40558293
For the end date, change
``````DateTime last = new DateTime(date.Year, 3 * Quarter - 2, 1).AddMonths(3).AddDays(-1);
``````
to
``````DateTime last = first.AddMonths(3).AddDays(-1);
``````
0

LVL 6

Author Closing Comment

ID: 40558325
Thanks Shaun
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
###### Suggested Courses
Course of the Month20 days, 20 hours left to enroll