Add Years to date

add years in date
In C# programming some times we  need to add years to the current year. This situation happens when we need to keep things in some date range. Means for example if I want to show date range from this year to next or next two years or specified years then we can use DateTime.AddYears Method.


DateTime AddYears Syntax:

public DateTime AddYears(int value);


Sample code that show how to add year in DateTime[C#] :

using System;
using System.Globalization;
public class DateTimeAddYear
{
public static void Main()
{
DateTime selectedDate = DateTime.Parse("2 Jan 2007");
selectedDate = selectedDate.AddYears(2);
Console.WriteLine(selectedDate.ToString("dd MMM yyyy"));   
}
}

//Output
//2 Jan 2009

0 Comments :

Post a Comment