Solved

Convert to DateTime (C#.NET 4.0)

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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