Solved

Convert to DateTime (C#.NET 4.0)

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

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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

13 Experts available now in Live!

Get 1:1 Help Now