Solved

Convert to DateTime (C#.NET 4.0)

Posted on 2012-03-30
3
529 Views
Last Modified: 2012-03-30
Hi,

I've got FROM and TO date textboxes that each have a nice calendar control.  The user can select the from and to dates to create a date range.

The values from the user come in as strings (e.g. "03/29/2012", "03/30/2012") and I convert them to DateTime with this code:

DateTime from = Convert.ToDateTime(dateFrom);
DateTime to = Convert.ToDateTime(dateTo);

The values of from and to become this:

"02/29/2012 12:00:00 AM"
"02/30/2012 12:00:00 AM"

The problem is that the last date above (the to date) should include all hours of this day (example: if the user selection 2/29/2012 and 2/30/2012, then the DateTimes I need are "02/29/2012 12:00:00 AM" and "02/30/2012 11:59:59 PM", so that all of 2/30/2012 are included.

What is the best, and most effective, way to format the last date (the to date) so that it includes the entire day?
0
Comment
Question by:adskarcox
  • 2
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 37787679
How about?

DateTime to = Convert.ToDateTime(dateTo);

to = to.AddDays(1).Date;  // Go to next day; zero out the time portion
to = to.AddSeconds(-1);   // Move back to previous day, last hour/minute

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37787849
I think to be more precise it should be to.AddTicks(-1) instead of to.AddSeconds(-1) otherwise events in the last second of the day would not be included due to the milliseconds/nanoseconds portion of DateTime.

DateTime to = Convert.ToDateTime(dateTo);

to = to.AddDays(1).Date;  // Go to next day; zero out the time portion
to = to.AddTicks(-1);   // Move back to previous day, last nano-second

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37787858
Agreed, but the question doesn't show that level of precision, so I went with Seconds   = )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now