Solved

Convert to DateTime (C#.NET 4.0)

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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