Solved

Convert to DateTime (C#.NET 4.0)

Posted on 2012-03-30
3
543 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
[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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

759 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