Solved

begin and end of DateTime day

Posted on 2011-02-15
6
513 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…

734 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