查找最近今天的周期性日期 - C# [英] Find recurring date nearest to today - C#
问题描述
基本上这是它应该如何工作,
StartDate:1/1/2012
完成日期:1/10/2012
重复周期:2(天)
输出将是:
1/6/2012
如果今天日期 Date.Now
)是 1/5/2012
(假设格式 MM / dd / yyyy
)。检查将在达到完成日期时结束。如果在指定时间段内没有任何日期匹配,则必须退回今天的日期。死的简单但不是一个有效的。
这是什么问题?
if(!_isRecurring)
return DateTime.UtcNow;
DateTime initialDate = _startDate;
DateTime finalDate = _finishDate;
int recurringDays = _recurringInteral;
/ *
*开始日期+循环间隔落在开始日期和完成日期之间,然后获取日期
* /
do
{
//添加循环day to start date
initialDate = initialDate.AddDays(recurringDays);
//检查是否在开始日期和结束日期之间
if(initialDate< = finalDate)
break;
} while(initialDate< = finalDate);
//返回循环日的第一次出现
return initialDate;
一点算术应该保存一天(双关语) :
var start = new DateTime(2012,1,1);
var end = new DateTime(2012,10,1);
var interval = 2; //天
var today = DateTime.Today;
var diff =(int)((today - start).TotalDays);
var mod = diff%interval;
var correction = TimeSpan.FromDays((mod> interval / 2?interval:0) - mod);
var result = today + correction>结束 ?今天:今天+修正
Console.Out.WriteLine(Result is:{0},result);
看到它在行动 。
这样做是计算今天复发地点的几天(可变 mod
)。这显然是一个数字> = 0和<间隔。如果是间隔的一半或更少,则意味着最近的重复点比今天早,在这种情况下,从今天起减去 mod
天。如果超过间隔时间的一半,这意味着我们需要添加 interval-mod
days来查找现在(将在将来)。
Sounds like a homework? no it's not. I worked up the logic for this but not such a performant one when dates are span over years. Basically here is how it should work,
StartDate: 1/1/2012
FinishDate: 1/10/2012
RecurringInterval: 2 ( In days)
Output would be:
1/6/2012
if Todays date (Date.Now
) is 1/5/2012
( Assuming format MM/dd/yyyy
). Check would end when finish date is reached. If no dates match within given time period, today's Date must be returned. Dead simple but not a efficient one.
What is wrong with this?
if (!_isRecurring)
return DateTime.UtcNow;
DateTime initialDate = _startDate;
DateTime finalDate = _finishDate;
int recurringDays = _recurringInteral;
/*
* start Date + recurring interval falls between start date and finishdate then get its date
*/
do
{
//add recurring day to start date
initialDate = initialDate.AddDays(recurringDays);
//check if it falls in between start days and end days
if(initialDate <= finalDate)
break;
} while (initialDate <= finalDate);
//return the first occurance of the recurring day
return initialDate;
A little arithmetic should save the day (pun intended):
var start = new DateTime(2012, 1, 1);
var end = new DateTime(2012, 10, 1);
var interval = 2; // days
var today = DateTime.Today;
var diff = (int)((today - start).TotalDays);
var mod = diff % interval;
var correction = TimeSpan.FromDays((mod > interval / 2 ? interval : 0) - mod);
var result = today + correction > end ? today : today + correction;
Console.Out.WriteLine("Result is: {0}", result);
What this does is calculate how many days away from a "recurrence spot" today is (variable mod
). This is obviously going to be a number >= 0 and < interval. If it's half the interval or less, it means the closest recurrence spot is earlier than today, in which case subtract mod
days from today to find the spot. If it's greater than half the interval, it means that we need to add interval - mod
days to find the spot (which is going to be in the future).
这篇关于查找最近今天的周期性日期 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!