查找最近今天重复日期 - 用C# [英] Find recurring date nearest to today - C#

查看:195
本文介绍了查找最近今天重复日期 - 用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听起来像是一门功课?不,这不对。我整理了一下逻辑这一点,但不是这样的高性能之一,当日期跨度年。基本上这里
是它如何工作,

 开始日期:1/1/2012 

FinishDate:2012/1/10

RecurringInterval:2(天)

输出是:



1/6/2012 如果今天的日期( Date.Now )是 2012/1/5 (假设格式 MM / DD / YYYY )。达到结束日期时,检查将结束。如果没有日期指定时段内匹配,今天的日期必须归还。死的简单,但不是有效率的。



什么是问题呢?



 如果(!_isRecurring) 
返回DateTime.UtcNow;
的DateTime initialDate = _startDate;
的DateTime finalDate = _finishDate;
INT recurringDays = _recurringInteral;
/ *
*开始日期+重复间隔落在开始日期之间finishdate然后得到它的日期
* /

{
//添加重复天开始日期
initialDate = initialDate.AddDays(recurringDays); //检查
如果它开始日和结束日期
之间落在如果(initialDate< = finalDate)
中断;
},而(initialDate< = finalDate);
//返回重复一天
返回initialDate中第一次出现;


解决方案

一个小算术应保存一天(双关语意)

  VAR开始=新日期时间(2012年,1,1); 
变种结束=新的datetime(2012,10,1);
VAR间隔= 2; //天今天

VAR = DateTime.Today;
VAR差异=(INT)((今 - 启动).TotalDays);
无功模=差异%区间;
无功补偿。= TimeSpan.FromDays(?(MOD>间隔/ 2间隔:0) - MOD);
VAR的结果=今天+修正>结束 ?今天:今天+修正;
Console.Out.WriteLine(结果是:{0},结果);



看到它在行动



这样做是当今计算多少天从一个现货复发离开是(变量 MOD )。这显然将是一个数> = 0和<间隔。如果是一半的时间或更少,这意味着最接近的复发现货比今天早些时候,在今天的情况下减 MOD 天才找到的地方。如果是超过一半的时间间隔越大,这意味着我们需要添加间隔 - MOD 天找点(这将是在未来)


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);

See it in action.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆