在我的C#应用程序中,我传递一个格式为yyyymmdd-yyyymmdd的字符串变量,它表示来自和迄今.我想分别得到这些日期的开始和结束时间.目前我有以下代码,但想知道是否有更优雅的解决方案?
所以对于pdr = 20090521-20090523会得到"20090521 00:00:00"和"20090523 23:59:59"
private void ValidateDatePeriod(string pdr, out DateTime startDate,
out DateTime endDate)
{
string[] dates = pdr.Split('-');
if (dates.Length != 2)
{
throw new Exception("Date period is of incorrect format");
}
if (dates[0].Length != 8 || dates[1].Length != 8)
{
throw new Exception("Split date periods are of incorrect format");
}
startDate = DateTime.ParseExact(dates[0] + " 00:00:00",
"yyyyMMdd HH:mm:ss", null);
endDate = DateTime.ParseExact(dates[1] + "23:59:59",
"yyyyMMdd HH::mm:ss", null);
}
Run Code Online (Sandbox Code Playgroud)
ana*_*lov 157
我很惊讶地看到一个错误的答案得到了如此多的赞成:
正确的版本如下:
public static DateTime StartOfDay(this DateTime theDate)
{
return theDate.Date;
}
public static DateTime EndOfDay(this DateTime theDate)
{
return theDate.Date.AddDays(1).AddTicks(-1);
}
Run Code Online (Sandbox Code Playgroud)
And*_*nea 35
您可以在某个实用程序类中定义两个扩展方法,如下所示:
public static DateTime EndOfDay(this DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
}
public static DateTime StartOfDay(this DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
然后在代码中使用它们:
public DoSomething()
{
DateTime endOfThisDay = DateTime.Now.EndOfDay();
}
Run Code Online (Sandbox Code Playgroud)
Tra*_*er1 18
如果你只是担心.Net精度......
startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddTicks(-1).AddDays(1);
Run Code Online (Sandbox Code Playgroud)
您实际上不需要将额外值连接到时间部分的字符串上.
作为附录,如果您使用它来查询例如数据库......
startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddDays(1);
Run Code Online (Sandbox Code Playgroud)
查询...
WHERE "startDate" >= @startDate AND "endDate" < @endDate
Run Code Online (Sandbox Code Playgroud)
那么评论中提到的精确问题就不重要了.在这种情况下,endDate不是范围的一部分,而是外边界.
小智 15
public static class DateTimeExtension {
public static DateTime StartOfTheDay(this DateTime d) => new DateTime(d.Year, d.Month, d.Day, 0, 0,0);
public static DateTime EndOfTheDay(this DateTime d) => new DateTime(d.Year, d.Month, d.Day, 23, 59,59);
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ell 14
该DateTime
对象具有一个属性Date
,该属性将仅返回日期部分.(时间部分默认为凌晨12:00).
我建议作为一个更优雅的解决方案(恕我直言),如果你想在最后一天允许任何日期时间,那么你添加1天到日期,并比较允许大于或等于开始日期的时间,但严格少于比结束日期(加1天).
// Calling code. beginDateTime and endDateTime are already set.
// beginDateTime and endDateTime are inclusive.
// targetDateTime is the date you want to check.
beginDateTime = beginDateTime.Date;
endDateTime = endDateTime.Date.AddDays(1);
if ( beginDateTime <= targetDateTime &&
targetDateTime < endDateTime )
// Do something.
Run Code Online (Sandbox Code Playgroud)
小智 5
我在 C# 中使用以下内容
public static DateTime GetStartOfDay(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0);
}
public static DateTime GetEndOfDay(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999);
}
Run Code Online (Sandbox Code Playgroud)
然后在 MS SQL 中,我执行以下操作:
if datepart(ms, @dateEnd) = 0
set @dateEnd = dateadd(ms, -3, @dateEnd)
Run Code Online (Sandbox Code Playgroud)
这将导致 MS SQL 时间为 23:59:59.997,这是成为第二天之前的最长时间。
你可以简单地使用:
new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999);
Run Code Online (Sandbox Code Playgroud)
这将在 MS SQL 中工作,但这在 .Net 方面并不准确。