简体   繁体   English

获取过去七天的日期

[英]Get date of last seven days

I want to get date of last seven days from now.For example current date is 我想从现在开始过去七天的日期。例如,当前日期是

02-10-2016, get date of seven days like this 02-10-2016,像这样得到七天的日期

01-10-2016,30-09-2016,29-09-2016,28-09-2016,27-09-2016,26-09-2016

My code 我的代码

string dt = DateTime.Now.ToString("yyyy-MM-dd");
DateTime lastWeek = dt.AddDays(-7.0);

AddDays is a part of DateTime , not of string . AddDaysDateTime的一部分,而不是string
You need to build your dates iteratively and then convert it to a string. 您需要迭代地构建日期,然后将其转换为字符串。

DateTime[] last7Days = Enumerable.Range(0, 7)
    .Select(i => DateTime.Now.Date.AddDays(-i))
    .ToArray();

foreach (var day in last7Days)
    Console.WriteLine($"{day:yyyy-MM-dd}"); // Any manipulations with days go here

Try using Linq : 尝试使用Linq

  var date = new DateTime(2016, 10, 2);

  var result = Enumerable.Range(1, 7)
    .Select(day => date.Date.AddDays(- day))
    .ToArray(); // if you want to represent dates as an array

Test 测试

  // 01-10-2016,30-09-2016,29-09-2016,28-09-2016,27-09-2016,26-09-2016,25-09-2016
  Console.Write(string.Join(",", result.Select(d => d.ToString("dd-MM-yyyy"))));

Without LINQ, with a simple loop: 没有LINQ,只需一个简单的循环:

DateTime dt = DateTime.Now;

for (int i=0;i<7;i++)
{
      dt = dt.AddDays(-1);
      Console.WriteLine(dt.Date.ToShortDateString());
}

You are almost there, the AddDays method will add only a specific number of days to the given data and dives you the resulted date. 您几乎就在那里,AddDays方法只会向给定数据添加特定天数,并将结果日期分给您。 But here in your case you need a list of dates, so you have to loop through those dates and get them as well. 但在这种情况下,你需要一个日期列表,所以你必须循环这些日期并获得它们。 I hope the following method will help you to do this: 我希望以下方法可以帮助您做到这一点:

 public static string GetLast7DateString()
 {
     DateTime currentDate = DateTime.Now;
     return String.Join(",",Enumerable.Range(0, 7)
                                      .Select(x => currentDate.AddDays(-x).ToString("dd-MM-yyyy"))
                                      .ToList());
 }

Note : If you want to exclude the current date means you have to take the range from 7 and the count should be 7 . 注意:如果要排除当前日期,则意味着必须从7 ,计数应为7 You can read more about Enumerable.Range here 您可以在此处阅读有关Enumerable.Range更多信息

If you call this method like the following means you will get the output as 24-10-2016,23-10-2016,22-10-2016,21-10-2016,20-10-2016,19-10-2016,18-10-2016 如果您按以下方式调用此方法,您将得到输出为24-10-2016,23-10-2016,22-10-2016,21-10-2016,20-10-2016,19-10-2016,18-10-2016

 string opLast7Days = GetLast7DateString();
 public static List<DateTime> getLastSevenDate(DateTime currentDate)
        {
            List<DateTime> lastSevenDate = new List<DateTime>();
            for (int i = 1; i <= 7; i++)
            {
                lastSevenDate.Add(currentDate.AddDays(-i));
            }
            return lastSevenDate;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM