本文整理汇总了C#中this.AddDays方法的典型用法代码示例。如果您正苦于以下问题:C# this.AddDays方法的具体用法?C# this.AddDays怎么用?C# this.AddDays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.AddDays方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Interval
public static DateTime Interval(this DateTime date, DateTimePart? intervalType, int? intervalVal)
{
if (intervalType.HasValue && intervalVal.HasValue)
{
switch (intervalType.Value)
{
case DateTimePart.Year:
return date.AddYears(intervalVal.Value);
case DateTimePart.Month:
return date.AddMonths(intervalVal.Value);
case DateTimePart.Day:
return date.AddDays((double)intervalVal.Value);
case DateTimePart.Hour:
return date.AddHours((double)intervalVal.Value);
case DateTimePart.Munite:
return date.AddMinutes((double)intervalVal.Value);
case DateTimePart.Second:
return date.AddSeconds((double)intervalVal.Value);
case DateTimePart.Week:
return date.AddDays((double)intervalVal.Value * 7);
case DateTimePart.Quarter:
return date.AddMonths(intervalVal.Value * 3);
}
}
return date;
}
示例2: AddMonths
public static DateTime AddMonths(this DateTime value, double months, bool ignoreSundays)
{
if (months < 0)
throw new NotImplementedException(); //# not implemented
if (months == 0)
{
int extraDaysToAdd = 0;
while (ignoreSundays && value.AddDays(extraDaysToAdd).DayOfWeek == DayOfWeek.Sunday)
extraDaysToAdd += 1;
return value.AddDays(extraDaysToAdd);
}
if (months < 1)
{
const int fixedDaysInMonth = 30;
int daysToAdd = (int)Math.Floor(fixedDaysInMonth * months);
int extraDaysToAdd = 0;
while (ignoreSundays && value.AddDays(daysToAdd + extraDaysToAdd).DayOfWeek == DayOfWeek.Sunday)
extraDaysToAdd += 1;
return value.AddDays(daysToAdd + extraDaysToAdd);
}
else
{
int monthsFloor = (int)Math.Floor(months);
return value.AddMonths(monthsFloor).AddMonths(months - monthsFloor, ignoreSundays);
}
}
示例3: GetWeekBeginAndEndTime
/// <summary>
/// 获取本周的开始时间和结束时间
/// </summary>
/// <param name="time">时间</param>
/// <param name="begin">返回本周的开始时间</param>
/// <param name="end">返回本周的结束时间</param>
public static void GetWeekBeginAndEndTime(this DateTime time, out DateTime begin, out DateTime end)
{
var inWeekCount = time.GetTimeInWeekCount();
if (inWeekCount == 0) inWeekCount = 7;
begin = time.AddDays(-(inWeekCount - 1));
end = time.AddDays((7 - inWeekCount));
}
示例4: FindClosestWeekDay
/// <summary>
/// Returns the closest Weekday (Financial day) Date
/// </summary>
/// <param name="obj">DateTime Base, from where the calculation will be preformed.</param>
/// <returns>Returns the closest Weekday (Financial day) Date</returns>
public static DateTime FindClosestWeekDay(this DateTime obj)
{
if (obj.DayOfWeek == DayOfWeek.Saturday)
return obj.AddDays(-1);
if (obj.DayOfWeek == DayOfWeek.Sunday)
return obj.AddDays(1);
return obj;
}
示例5: ToNearestWorkingDay
public static DateTime ToNearestWorkingDay(this DateTime date)
{
switch (date.DayOfWeek)
{
case DayOfWeek.Saturday: return date.AddDays(2);
case DayOfWeek.Sunday: return date.AddDays(1);
default: return date;
}
}
示例6: ClosestBusinessDay
/// <summary>
/// Extension method used to determine the closest working day to a specific date.
/// </summary>
/// <param name="dateToCheck">The DateTime variable that will use the extenison method.</param>
/// <returns>A DateTime value that corresponds to the closest business day
/// in relation to the supplied date. This does not take into consideration
/// any holidays and assumes a Monday to Friday business week.</returns>
public static DateTime ClosestBusinessDay(this DateTime dateToCheck)
{
if (dateToCheck.DayOfWeek == DayOfWeek.Saturday)
return dateToCheck.AddDays(-1);
else if (dateToCheck.DayOfWeek == DayOfWeek.Sunday)
return dateToCheck.AddDays(1);
else
return dateToCheck;
}
示例7: GetLastDateOfWeek
/// <summary>
/// Get the last date of the week for a certain date.
///
/// Note that for ISO 8601 dates, iso8601 must be set to true.
/// </summary>
public static DateTime GetLastDateOfWeek(this DateTime date, bool iso8601 = false, CalendarWeekRule weekRule = CalendarWeekRule.FirstFourDayWeek, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
{
if (date == DateTime.MaxValue)
return date;
var week = date.GetWeekNumber(iso8601, weekRule, firstDayOfWeek);
while (week == date.GetWeekNumber(iso8601, weekRule, firstDayOfWeek))
date = date.AddDays(1);
return date.AddDays(-1);
}
示例8: AddDaysSkippingWeekends
private static DateTime AddDaysSkippingWeekends(this DateTime d, int numberOfDays, IEnumerable<DateTime> skipDays)
{
var nextDay = d.AddDays(numberOfDays);
if (nextDay.DayOfWeek == DayOfWeek.Saturday || nextDay.DayOfWeek == DayOfWeek.Sunday || (skipDays != null && skipDays.Any(date => date == nextDay)))
{
return d.AddDaysSkippingWeekends(numberOfDays + 1, skipDays);
}
return d.AddDays(numberOfDays);
}
示例9: PreviousOccurance
/// <summary>
/// Returns the last occurance of the specified day
/// </summary>
/// <param name="dt">Start date</param>
/// <param name="day">Day to find</param>
/// <param name="ignoreCurrent">Ignore the current day if it matches</param>
/// <returns></returns>
public static DateTime PreviousOccurance(this DateTime dt, DayOfWeek day, bool ignoreCurrent = true)
{
if (!ignoreCurrent && dt.DayOfWeek == day)
return dt.AddDays(-7);
while (dt.DayOfWeek != day)
{
dt = dt.AddDays(-1);
}
return dt;
}
示例10: ToEndOfBusinessWeek
public static DateTime ToEndOfBusinessWeek(this DateTime endOfWeek)
{
while (endOfWeek.DayOfWeek == DayOfWeek.Saturday || endOfWeek.DayOfWeek == DayOfWeek.Sunday)
{
endOfWeek = endOfWeek.AddDays(-1);
}
while (endOfWeek.DayOfWeek != DayOfWeek.Friday)
{
endOfWeek = endOfWeek.AddDays(1);
}
return endOfWeek;
}
示例11: SkipToNextWorkDay
public static DateTimeOffset SkipToNextWorkDay(this DateTimeOffset date)
{
// we explicitly choose not to user the CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
// because we want it to be fixed for what we need, not whatever the user for this is set to.
if (date.DayOfWeek == DayOfWeek.Saturday)
return date.AddDays(2);
if (date.DayOfWeek == DayOfWeek.Sunday)
return date.AddDays(1);
return date;
}
示例12: GetNextWeekDay
/// <summary>
/// Gets the next week day after the given date
/// </summary>
public static DateTime GetNextWeekDay(this DateTime date)
{
if (date.DayOfWeek == DayOfWeek.Friday)
{
return date.AddDays(3);
}
if (date.DayOfWeek == DayOfWeek.Saturday)
{
return date.AddDays(2);
}
return date.AddDays(1);
}
示例13: GetPreviousWeekDay
/// <summary>
/// Gets the previous week day before the given date
/// </summary>
public static DateTime GetPreviousWeekDay(this DateTime date)
{
if (date.DayOfWeek == DayOfWeek.Monday)
{
return date.AddDays(-3);
}
if (date.DayOfWeek == DayOfWeek.Sunday)
{
return date.AddDays(-2);
}
return date.AddDays(-1);
}
示例14: NextNWeekday
internal static DateTime NextNWeekday(this DateTime current, int toAdvance)
{
while (toAdvance >= 1)
{
toAdvance--;
current = current.AddDays(1);
while (!current.IsWeekday())
current = current.AddDays(1);
}
return current;
}
示例15: resolveDateTime
/// <summary>
/// –азрешает дату, относительно заданной
/// </summary>
/// <param name="source">исходна¤ дата</param>
/// <param name="resolver">смещение</param>
/// <returns></returns>
public static DateTime resolveDateTime(this DateTime source, string resolver)
{
//если пр¤мо указана дата - берем ее
DateTime directDate = new DateTime();
var fullDefinition = DateTime.TryParseExact(resolver, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out directDate);
if (fullDefinition)
{
return directDate;
}
//если цифры, то "за столько дней назад"
if (resolver.like(@"^-?\d+$")) return source.AddDays(-Int32.Parse(resolver));
var c = 1;
//если W -то "с начала недели", цифра показывает за сколько последних недель
if (resolver.like(@"^[Ww]\d*"))
{
var cnt = resolver.find(@"\d+");
if (cnt.hasContent())
{
c = Int32.Parse(cnt);
}
var ds = 1 + (int)source.DayOfWeek;
if (ds == 7) ds = 0;
return source.AddDays(-7 * (c - 1) - ds);
}
//если M -то "с начала мес¤ца", цифра показывает за сколько последних мес¤цев
if (resolver.like(@"^[Mm]\d*"))
{
var cnt = resolver.find(@"\d+");
if (cnt.hasContent())
{
c = Int32.Parse(cnt);
}
return new DateTime(source.Year, source.Month, 1).AddMonths(-(c - 1));
}
//если Y -то "с начала года", цифра показывает за сколько последних годов
if (resolver.like(@"^[Yy]\d*"))
{
var cnt = resolver.find(@"\d+");
if (cnt.hasContent())
{
c = Int32.Parse(cnt);
}
return new DateTime(source.Year, 1, 1).AddYears(-(c - 1));
}
throw new InvalidOperationException("Ќеверный формат резольвера - " + resolver);
}