.net 如何使用 DateTime 指定一天中的最晚时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I specify the latest time of day with DateTime
提问by Joe Phillips
I am using a System.DateTimeobject to allow a user to select a date range. The user is only able to select a date (not time) using a third party calendar so I will need to automatically specify the time of day it should use (ie: 00:00:00 or 23:59:59) after the date is chosen.
我正在使用一个System.DateTime对象来允许用户选择日期范围。用户只能使用第三方日历选择日期(而不是时间),因此我需要在日期之后自动指定它应该使用的时间(即:00:00:00 或 23:59:59)被选中。
How can I specify the time after the date is already stored as a DateTime object by the calendar selector? I could use the AddHours, AddMinutes, AddSecondsmethods but those are relative to the current time which may not be 00:00:00.
如何在日历选择器将日期存储为 DateTime 对象之后指定时间?我可以使用这些AddHours, AddMinutes, AddSeconds方法,但这些方法与当前时间相关,可能不是 00:00:00。
The startDatewill need to have a time of 00:00:00 and endDatehave a time of 23:59:59 to account for the entire days.
在startDate将需要有时间是00:00:00,并endDate有一23:59:59时间占到整个天。
回答by Jose Basilio
If you already have a DateTimeobject created and want to replace the time with the 11:59:59PM for that given date, then you can use the .Dateproperty to get the date with time set to 00:00:00 and then add the hours, minutes and seconds. For example:
如果您已经DateTime创建了一个对象并希望用该给定日期的 11:59:59PM 替换时间,那么您可以使用该.Date属性获取时间设置为 00:00:00 的日期,然后添加小时数,分钟和秒。例如:
var dt = yourDateInstance.Date.AddHours(23).AddMinutes(59).AddSeconds(59);
If by latest time, you mean 11:59:59 PM, then this should also work:
如果按最晚时间,您的意思是晚上 11:59:59,那么这也应该有效:
var dt = new DateTime(Now.Year, Now.Month, Now.Day, 23, 59, 59);
回答by Jon B
To get the last instant for today:
获取今天的最后一刻:
DateTime d = new DateTime(Now.Year, Now.Month, Now.Day);
d = d.AddDays(1);
d = d.AddTicks(-1);
In response to your edit, here's what I would do:
为了回应您的编辑,我会这样做:
DateTime start = new DateTime(Now.Year, Now.Month, Now.Day);
DateTime end = start.AddDays(1).AddTicks(-1);
// Or - just use end = start.AddDays(1), and use a < for comparison
回答by Peter Drier
DateTime d = DateTime.Today.AddDays(1).AddTicks(-1);
回答by Joe
Your question has already been answered, but IMHO a better way is not to bother attempting to subtract a tick, a second, or whatever from the end of the range, and compare using strictly less than.
您的问题已经得到解答,但恕我直言,更好的方法是不要费心尝试从范围的末尾减去一个刻度、一秒或任何其他内容,并严格使用小于进行比较。
So that if you want all dates in an inclusiverange from startDate to endDate, ignoring the time, you could use the following in C#:
因此,如果您想要从 startDate 到 endDate的包含范围内的所有日期,忽略时间,您可以在 C# 中使用以下内容:
if ((myDate >= startDate.Date) && (myDate < endDate.Date.AddDays(1)))
{
// ... it's in the range
}
or in T-SQL, assuming your @StartDate and @EndDate are exactly midnight, something like:
或在 T-SQL 中,假设您的 @StartDate 和 @EndDate 正好是午夜,例如:
WHERE SomeDate >= @StartDate AND SomeDate < DATEADD(d,1,@EndDate)
UPDATE
更新
Updated example to show an inclusive range in response to comments.
更新示例以显示包含范围以响应评论。
回答by Erich Mirabal
DateTime startDate = DateTime.Today;
DateTime stopDate = startDate.AddDays(1).AddTicks(-1);
As a note, DateTime.Todayreturns (from MSDN)
注意,DateTime.Today返回(来自 MSDN)
A System.DateTime set to today's date, with the time component set to 00:00:00.
System.DateTime 设置为今天的日期,时间组件设置为 00:00:00。
So as others have noted, add a day, then subtract the smallest time quantum (a tick), and you get the last possible time for the current day.
因此,正如其他人所指出的,添加一天,然后减去最小的时间量(一个刻度),您将获得当天的最后可能时间。
Of course, you might have to think about TimeZones and such depending where the code runs versus where the user is. UTC time might be good, but that might bump you off a day (either way) depending where your code runs.
当然,您可能必须考虑 TimeZones 等,具体取决于代码运行的位置与用户的位置。UTC 时间可能不错,但这可能会让您休息一天(无论哪种方式),具体取决于您的代码运行位置。
回答by Hein Andre Gr?nnestad
Based on the other answers I created this convenient extension method:
基于其他答案,我创建了这个方便的扩展方法:
public static class DateTimeExtensions
{
public static DateTime EndOfDay(this DateTime dateTime)
{
return dateTime.Date.AddDays(1).AddTicks(-1);
}
}
回答by 0x49D1
For example
例如
DateTime.Now.Date.AddDays(1).AddSeconds(-1)
Or AddTicks/AddMilliseconds/AddMinutes... based on the precision you need.
或者AddTicks/AddMilliseconds/AddMinutes...基于您需要的精度。
回答by Mohammad Javad Tavakoli
use this
用这个
DateTime YourNewDate = new DateTime(YourDate .Year, YourDate .Month, YourDate .Day, 23, 59, 59, 99);
DateTime YourNewDate = new DateTime(YourDate .Year, YourDate .Month, YourDate .Day, 23, 59, 59, 99);
回答by dunwan
yourDateInstance.CloseDate = yourDateInstance.CloseDate.Date.AddDays(1).AddMilliseconds(-1);
yourDateInstance.CloseDate = yourDateInstance.CloseDate.Date.AddDays(1).AddMilliseconds(-1);
回答by Md. Tazbir Ur Rahman Bhuiyan
this will give you the expected result:
这会给你预期的结果:
DateTime startDate= DateTime.Now.Date;
DateTime endDate= startDate.AddDays(2).Add(new TimeSpan(23, 59, 59));
//startDate: 28/9/2017 0:0:0 endDate: 29/9/2017 23:59:59

