Solved

begin and end of DateTime day

Posted on 2011-02-15
6
516 Views
Last Modified: 2012-05-11
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
Comment
Question by:lavitz
[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
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
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
http://msdn.microsoft.com/en-us/library/system.datetime.adddays%28v=vs.71%29.aspx

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

Expert Comment

by:Shaun Kline
ID: 34899565
Do you need end of day, or start of next day?
This formula does start of next day:
DATEADD(dd, 1, DATEDIFF(dd, 0, GETDATE()))

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

Start of next month is:
DATEADD(dd, -DAY(GETDATE()) + 1, DATEADD(mm, 1, DATEDIFF(dd, 0, GETDATE())))
0
 
LVL 20

Expert Comment

by:informaniac
ID: 34899566
I'm nt sure I understood ur question. Can u rephrase it?
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 44

Expert Comment

by:AndyAinscow
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

by:lavitz
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

by:
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);

Open in new window


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");

DateTime endOfDay = testDate.Date.AddDays(1).AddSeconds(-1);

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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