Solved

begin and end of DateTime day

Posted on 2011-02-15
6
481 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
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
I'm nt sure I understood ur question. Can u rephrase it?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>'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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 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

10 Experts available now in Live!

Get 1:1 Help Now