Solved

# begin and end of DateTime day

Posted on 2011-02-15
500 Views
Hi,

Does DataTime have something that allow me from DateTime '2011-02-15 15:44' make end of day?
I yes. How to do it for day, week, month?
0
Question by:lavitz

LVL 142

Expert Comment

ID: 34899539
you could construct a new datetime value from it's year, month and day values, and add 1 day to that:
http://msdn.microsoft.com/en-us/library/aa326683%28v=vs.71%29.aspx

knowing that 1 day is worth
>= start of day  AND < start of next day
0

LVL 26

Expert Comment

ID: 34899565
Do you need end of day, or start of next day?
This formula does start of next day:

Start of next week is:
DATEADD(dd, 8 - DATEPART(dw, getdate()), DATEDIFF(dd, 0, GETDATE()))

Start of next month is:
0

LVL 20

Expert Comment

ID: 34899566
I'm nt sure I understood ur question. Can u rephrase it?
0

LVL 44

Expert Comment

ID: 34899630
>>'2011-02-15 15:44' make end of day?

From a datetime such as the above do you want to know how much time it is before the next day/week/month ?
0

Author Comment

ID: 34899703
from date '2011-01-01 12:14:44' i need end of that day so output should be '2011-01-01 23:59:59'. I know i could do this by adding '23:59:59' but i wonder if .net have such function (and for end of month, year, week)
0

LVL 33

Accepted Solution

Todd Gerbert earned 500 total points
ID: 34899979
Well, since the end of the day (to my knowledge) is always, always, always 23:59:59 you could just use the .Year, .Month and .Day components of your starting date and pass'em to the constructor for a new DateTime with 23:59:59 hard-coded for the time portion.

``````DateTime testDate = DateTime.Parse("2011-01-01 12:14:44");

DateTime endOfDay = new DateTime(testDate.Year, testDate.Month, testDate.Day, 23, 59, 59);
``````

Or use just the Date portion of testDate, add one day, then subtract one second:
``````DateTime testDate = DateTime.Parse("2011-01-01 12:14:44");

``````
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…