你如何在C#中的日期字段上进行周末舍入(不使用LINQ)?

时间:2022-09-29 07:17:21

I have a weird date rounding problem that hopefully someone can solve. My client uses a work week that runs from Monday through Sunday. Sunday's date is considered the end of the week, and is used to identify all records entered in a particular week (so anything entered last week would have a WEEKDATE value of '10/26/2008', which is Sunday's date).

我有一个奇怪的日期舍入问题,希望有人可以解决。我的客户使用从周一到周日的工作周。星期日的日期被视为本周结束,用于识别在特定周内输入的所有记录(因此上周输入的任何内容的WEEKDATE值为'10 / 26/2008',即星期日的日期)。

One little twist is that users enter records for the previous week up until 11 AM on the Monday of the current week.

一个小小的转折是用户输入前一周的记录,直到本周一的星期一上午11点。

So I need a function that starts with DateTime.Now and returns the week-ending date (no time part) according to the rules above. Thanks for your help. I have a solution that works, but I'm too embarassed to post it.

所以我需要一个以DateTime.Now开头的函数,并根据上面的规则返回周末日期(没有时间部分)。谢谢你的帮助。我有一个有效的解决方案,但我很尴尬发布它。

Oh, and I can't use LINQ.

哦,我不能使用LINQ。

4 个解决方案

#1


4  

public DateTime WeekNum(DateTime now)
{
    DateTime NewNow = now.AddHours(-11).AddDays(6);

    return (NewNow.AddDays(- (int) NewNow.DayOfWeek).Date);
}

public void Code(params string[] args)
{

    Console.WriteLine(WeekNum(DateTime.Now));   
    Console.WriteLine(WeekNum(new DateTime(2008,10,27, 10, 00, 00)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,27, 12, 00, 00)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,28)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,25)));


}

You may hard-code DateTime.Now instead of passing a DateTime object. It just made testing easier this way.

您可以硬编码DateTime.Now而不是传递DateTime对象。它只是通过这种方式使测试更容易。

#2


4  

This passes for me as well:

这也传递给我:

[Test]
public void Test()
{
   DateTime sunday = DateTime.Parse("10/26/2008");
   DateTime nextSunday = DateTime.Parse("11/2/2008");

   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/21/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/22/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/23/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/24/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/25/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/26/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/27/2008 10:59 AM")));
   Assert.AreEqual(nextSunday, GetSunday(DateTime.Parse("10/27/2008 11:00 AM")));
}

private DateTime GetSunday(DateTime date)
{
   if (date.DayOfWeek == DayOfWeek.Monday && date.Hour < 11)
      return date.Date.AddDays(-1);

   while (date.DayOfWeek != DayOfWeek.Sunday)
      date = date.AddDays(1);

   return date.Date;
}

#3


1  

DateTime GetMidnightFollowingSunday()
{
    DateTime now = DateTime.Now;
    return now.AddDays(7 - (int)now.DayOfWeek).Date;
}

If you need to start the new week after 11AM on Monday morning just subtract 11 hours from now but then it probably makes sense to name the method something else.

如果你需要在星期一早上11点之后开始新的一周,那么从现在开始减去11个小时,但是将方法命名为其他东西可能是有意义的。

DateTime GetRecordDate()
{
    DateTime nowMinusOffset = DateTime.Now.AddHours(-11);
    return nowMinusOffset.AddDays(7-(int)nowMinusOffset.DayOfWeek).Date;
}

#4


0  

I've used these extensions with great success:

我使用这些扩展非常成功:

http://www.codeplex.com/DateTimeExtensions

#1


4  

public DateTime WeekNum(DateTime now)
{
    DateTime NewNow = now.AddHours(-11).AddDays(6);

    return (NewNow.AddDays(- (int) NewNow.DayOfWeek).Date);
}

public void Code(params string[] args)
{

    Console.WriteLine(WeekNum(DateTime.Now));   
    Console.WriteLine(WeekNum(new DateTime(2008,10,27, 10, 00, 00)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,27, 12, 00, 00)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,28)));
    Console.WriteLine(WeekNum(new DateTime(2008,10,25)));


}

You may hard-code DateTime.Now instead of passing a DateTime object. It just made testing easier this way.

您可以硬编码DateTime.Now而不是传递DateTime对象。它只是通过这种方式使测试更容易。

#2


4  

This passes for me as well:

这也传递给我:

[Test]
public void Test()
{
   DateTime sunday = DateTime.Parse("10/26/2008");
   DateTime nextSunday = DateTime.Parse("11/2/2008");

   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/21/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/22/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/23/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/24/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/25/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/26/2008")));
   Assert.AreEqual(sunday, GetSunday(DateTime.Parse("10/27/2008 10:59 AM")));
   Assert.AreEqual(nextSunday, GetSunday(DateTime.Parse("10/27/2008 11:00 AM")));
}

private DateTime GetSunday(DateTime date)
{
   if (date.DayOfWeek == DayOfWeek.Monday && date.Hour < 11)
      return date.Date.AddDays(-1);

   while (date.DayOfWeek != DayOfWeek.Sunday)
      date = date.AddDays(1);

   return date.Date;
}

#3


1  

DateTime GetMidnightFollowingSunday()
{
    DateTime now = DateTime.Now;
    return now.AddDays(7 - (int)now.DayOfWeek).Date;
}

If you need to start the new week after 11AM on Monday morning just subtract 11 hours from now but then it probably makes sense to name the method something else.

如果你需要在星期一早上11点之后开始新的一周,那么从现在开始减去11个小时,但是将方法命名为其他东西可能是有意义的。

DateTime GetRecordDate()
{
    DateTime nowMinusOffset = DateTime.Now.AddHours(-11);
    return nowMinusOffset.AddDays(7-(int)nowMinusOffset.DayOfWeek).Date;
}

#4


0  

I've used these extensions with great success:

我使用这些扩展非常成功:

http://www.codeplex.com/DateTimeExtensions