c# - DateTime shift to next predefined date -
say have list of valid scheduling days. like: 23, 27, 29
i want modify given date it's next valid day-month based on above list.
if given date "23/11/2013" next valid date "27/11/2013" if given date "30/11/2013" has return "23/12/2013" , if given date "30/12/2013" has return "23/01/2014"
i've done in sql, i'm translating c# , it's bit tricky. i'm trying using linq on list sql similarity, gets confusing.
the sql statement (yes, know doesn't swift year):
select top 1 @date = isnull(dateadd(yy, year(@date)-1900, dateadd(m, (month(@date)+case when datepart(day,@date)>[day] 1 else 0 end) - 1, [day] - 1)),@date) @days dateadd(yy, year(@date)-1900, dateadd(m, (month(@date)+case when datepart(day,@date)>[day] 1 else 0 end) - 1, [day] - 1))>=@date order [day]
@days working table.
i think you're looking (using linq):
public static datetime nextdate(datetime seed, int[] days) { if (enumerable.range(1, 31).intersect(days).any()) //check stop long running lookup! { return enumerable.range(0, int.maxvalue) .select(i => seed.adddays(i)) .first(d => days.contains(d.day)); } return seed; }
usage:
var nextdate = nextdate(datetime.now, new[] { 23, 27, 29 });
Comments
Post a Comment