The Date AddTenor function is used in C# programming language to add a particular number of business days or calendar days to a given date. It returns a new date that is the result of adding the specified number of business or calendar days to the original date.
Example 1: Adding 10 business days to a given date
using System;
public static class DateExtensions { public static DateTime AddBusinessDays(this DateTime date, int days) { int sign = Math.Sign(days); int unsignedDays = Math.Abs(days); int completeWeeks = unsignedDays / 5; int weekdays = unsignedDays % 5;
if (date.DayOfWeek == DayOfWeek.Saturday) { date = date.AddDays(2 * sign); } else if (date.DayOfWeek == DayOfWeek.Sunday) { date = date.AddDays(1 * sign); }
int remainingDays = weekdays + completeWeeks * 7; for (int i = 0; i < remainingDays; i++) { date = date.AddDays(sign); while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) { date = date.AddDays(sign); } } return date; } }
DateTime startDate = new DateTime(2021, 2, 15); DateTime endDate = startDate.AddBusinessDays(10); // Add 10 business days Console.WriteLine(endDate); // Output: 3/1/2021 12:00:00 AM
In this example, we have defined an extension method called AddBusinessDays(), which takes a DateTime object and an integer representing the number of business days to add. The algorithm first determines if the starting date is a weekend day and adjusts accordingly. It then adds the required number of business days by adding a day at a time and skipping weekends until the desired number of business days have been added.
Example 2: Adding 10 calendar days to a given date
DateTime startDate = new DateTime(2021, 2, 15); DateTime endDate = startDate.AddDays(10); // Add 10 calendar days Console.WriteLine(endDate); // Output: 2/25/2021 12:00:00 AM
In this example, we use the built-in AddDays() method of the DateTime class to add 10 calendar days to the starting date.
The Date AddTenor function is not a built-in function in C# programming language. It needs to be either included in the project's source code or as a dependency package library. Therefore, the package library for the Date AddTenor function cannot be determined without more information.
C# (CSharp) Date.AddTenor - 19 examples found. These are the top rated real world C# (CSharp) examples of Date.AddTenor from package study extracted from open source projects. You can rate examples to help us improve the quality of examples.