-1

我在 C#.Net 中工作。我需要从日期和截止日期。例如:如果我选择“上周”意味着,我应该得到 4 月 15 日 - 4 月 21 日。“2 周前”是指 4 月 8 日至 4 月 14 日。与 3 周前、4 周前等相同。

如何获取 From Date 和 To Date..

4

2 回答 2

0
  var currentWeek_StartDate = GetStartWeekDate_By_Date(DateTime.Now.Date);

  var lastWeek_StartDate = GetStartWeekDate_By_WeekIndex(currentWeek_StartDate, -1);
  var lastWeek_EndDate = GetEndWeekDate_By_StartWeekDate(lastWeek_StartDate);


public static DateTime GetStartWeekDate_By_Date(DateTime date)
{
  return date.AddDays(-(int)date.DayOfWeek);
}
public static DateTime GetEndWeekDate_By_StartWeekDate(DateTime date)
{
  return date.AddDays(6);
}
public static DateTime GetStartWeekDate_By_WeekIndex(DateTime date, int weekIndex)
{
  return date.AddDays(weekIndex * 7);
}
于 2012-04-27T11:04:50.353 回答
0

我做了一个控制台程序..itz 工作正常.. 看看

static void Main(string[] args)
        {
            Console.WriteLine("Enter week number");
            int week = int.Parse(Console.ReadLine());
            var weekStartDay = DayOfWeek.Monday;
            int daysInAWeek = 7;
            DateTime thisWeekStarttingDate = DateTime.Now.Subtract(new TimeSpan((int)DateTime.Now.DayOfWeek - (int)weekStartDay, 0, 0, 0)).Date;

            DateTime fromDate = thisWeekStarttingDate.Subtract(new TimeSpan(daysInAWeek * week, 0, 0, 0));
            DateTime toDate = fromDate.AddDays(daysInAWeek-1);

            Console.WriteLine("from date:" + fromDate.ToShortDateString());
            Console.WriteLine("to date:" + toDate.ToShortDateString());
            Console.ReadKey();
        }
于 2012-04-27T11:09:39.200 回答