?
Solved

Setting DateTimePicker to last quarter

Posted on 2015-01-19
5
Medium Priority
?
299 Views
Last Modified: 2015-01-19
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);
                        DateTime last = new DateTime(date.Year, 3 * Quarter - 2, 1).AddMonths(3).AddDays(-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");

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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 26

Accepted Solution

by:
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);

Open in new window

to
DateTime first = new DateTime(date.Year, 3 * Quarter - 2, 1).AddMonth(-3);

Open in new window

to get the prior quarter start date.
0
 
LVL 6

Author Comment

by:r3nder
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 26

Assisted Solution

by:Shaun Kline
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);

Open in new window

to
DateTime last = first.AddMonths(3).AddDays(-1);

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40558325
Thanks Shaun
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Starting up a Project
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question