简体   繁体   English

C#:DateTime - 经历一段时间?

[英]C#: DateTime - Going through a period of days?

I have a period of days and I want to go through it and execute the same code on each date. 我有一段时间,我想通过它并在每个日期执行相同的代码。

begin and end are DateTime format with difference of a month at least 开始结束DateTime格式,至少相差一个月

while ( !(begin.Equals(end)) )
        {
           ...some code here...              
           begin = begin.AddDays(1);
        }
  1. I'm not sure if it automatically upgrades the Month value when the Day value reaches the end of an exact month(in exact year) - for example February doesn't have always the same amount of days so... 我不确定当Day值到达确切月份的结尾时(确切的年份)它是否会自动升级Month值 - 例如2月并不总是相同的天数所以...

  2. Is there a better/shorter/nicer way of increasing the date by one day? 是否有更好/更短/更好的方式将日期增加一天? For example something like this: begin.Day++ ; 比如这样的事情: begin.Day++ ; or this: begin++; 或者这个: begin++; ?

I'm not used to C# yet so sorry for asking this lame question and thank you in advance for any answer. 我不习惯C#,但很抱歉问这个蹩脚的问题,并提前感谢您的回答。

1) Yes it does. 1)是的确如此。 All date arithmetic is handled correctly for you. 所有日期算术都可以正确处理。

2) Yes there is. 2)是的。 You can do: 你可以做:

var oneDay = TimeSpan.FromDays(1);
...
begin += oneDay;

You could also use a for loop: 你也可以使用for循环:

var oneDay = TimeSpan.FromDays(1);

for (DateTime currentDay = begin; currentDay < end; currentDay += oneDay)
{
    // Some code here.
}

One final thing: If you want to be sure to ignore the time component, you can ensure that the time part of the begin and end dates is set to midnight as follows: 最后一件事:如果您想确保忽略时间组件,可以确保将开始日期和结束日期的时间部分设置为午夜,如下所示:

begin = begin.Date;
end   = end.Date;

Make sure you have your bounds correct. 确保你的界限正确。 The loop goes while currentDay < end - but you might need currentDay <= end if your time range is inclusive rather than exclusive. currentDay < end循环currentDay < end - 但如果你的时间范围是包容性而不是排他性,你可能需要currentDay <= end

Do it this way (don't compare for equality, because hours may be different and the loop goes forever). 这样做(不要比较相等,因为小时可能不同,循环永远不变)。

    while ( begin <= end )
    {
       ...some code here...              
       begin = begin.AddDays(1);
    }

The code you've posted is correct and should work fine. 您发布的代码是正确的,应该可以正常工作。 And don't worry, the AddDays method will automatically increment the month and the year when necessarry. 不用担心, AddDays方法会在必要时自动增加月份和年份。

You can also use a for loop if you find it more readable: 如果你发现它更具可读性,你也可以使用for循环:

for (DateTime date = startDate; date < endDate; date = date.AddDays(1))
{
    // Your code here
}

You could try this, which I think is a little more succinct: 你可以试试这个,我觉得它更简洁一些:

while (DateTime.Compare(begin, end) < 0)
{
    /* Some code here */
    begin = begin.AddDays(1);
}

The DateTime object knows how to increment months, years, etc. as appropriate, so you needn't worry about that. DateTime对象知道如何适当增加月,年等,因此您不必担心这一点。

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

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