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

Add days in date

add days to datetime

Some times we need to add days in the date. This is needed when we want to give some days duration to perform some actions based on date.For example if we want to publish some article for two days and then hide or delete it after two days.So to add days in given date we need to use DateTime.AddDays Method


Syntax:


public DateTime AddDays(double value)

Sample code to add days in date[C#]:


using System;
using System.Globalization;
public class DateTimeAddYear
{
public static void Main()
{
DateTime selectedDate = DateTime.Parse("12 Jan 2012");
selectedDate = selectedDate.AddDays(2);
Console.WriteLine(selectedDate.ToString("dd MMM yyyy")); 
}
}//Output
//14 Jan 2012

Get Today’s Date

Get today's date
When dealing with dates most of the time we need to get current date or today’s date in C# code to perform some operations. DateTime class provides DateTime.Today property to get today’s date. Today property returns current date only and does not include time in it so we can use Today property in code where we just need today’s date and not time. Then we can use .ToString() to format string date as per our need.


Sample code to get today’s or current date [C#]


using System;
using System.Globalization;
public class DateTimeAddYear
{
public static void Main()
{
// Get current date.
DateTime todaysDate = = DateTime.Today;
//And show today's date in needed format
Console.WriteLine(todaysDate.ToString("dd MMM yyyy"));
}
}
//Output
//12 Jan 2012

Get only date part from datetime

get date part only datetime
In some cases we need to get the date with date and time with midnight time like 00:00:00 in C# code to perform some operations. DateTime class provides DateTime.Date property to get date and time with time like 00:00:00.

Sample code to get today’s or current date [C#]


using System;
using System.Globalization;
public class DateTimeAddYear
{   
public static void Main()
{
DateTime selectedDate = DateTime.Parse("12 Jan 2012");
Console.WriteLine(selectedDate.Date);  
}
}
//Output
//2 Jan 2009

Get full name of the day C# datetime

get day name datetime c#
In most C# programs while formatting the dates we need to show complete or full name of the day. Here we are going to see how “dddd” format specifier is used to show full name of the day.

Sample code to show the full name of the day [C#]

using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime dateToFormat =  new DateTime(2008, 8, 29, 19, 27, 15); 
Console.WriteLine(dateToFormat.ToString("dddd"));
}
}
//Output
//Friday

Date in Month Day, Year Format (MMMM dd, yyyy)

format datetime in Month Day, Year
Some times we need to show date in Month Day, Year Format like January 12, 2012. Here we will see how to format DateTime using string formatter.

Sample code to show date time in Month Day, Year format [C#]


using System;
using System.Globalization;

public class DateTimeFomrateString
{
 public static void Main()
 {
  DateTime datetoFormat = new DateTime(2012, 1, 12);
  Console.WriteLine("Today's Date is " + datetoFormat.ToString("MMMM dd, yyyy") + ".");
 }
}
//Code will display the following output:
//Today's Date is June 10, 2011.

Here MMMM is format specifier that represents month as name like for 05 it will display May. dd is format specifier from day of the month with digits from 01 to 31 and yyyy is the format specifier with four digit number like 2012.

Date in MM-dd-yy format

get date in MM-dd-yy format
Most of the time in C# code we need to format datetime as per requirement. Here we are going to discuss how to format date in MM-dd-yy format.So when we give input date as “10-29-11” then date will get formatted like “10/29/11”.

Sample code to format date in MM-dd-yy format [C#] :


using System;
using System.Globalization;

public class DateFormatSample
{
 public static void Main()
 {
  string dateToFormat = "10-29-11";
  string strFormatPattern = "MM-dd-yy";
  DateTime formattedDate;
  DateTime.TryParseExact(dateToFormat, strFormatPattern, null, 
  DateTimeStyles.None, out formattedDate);
  Console.WriteLine("Date '{0}' Formatted to {1:d}.", dateToFormat, formattedDate);
 }
}
//    output:
//    Date '12-30-11' Formatted to 12/30/2011.

Here MM displays in format 01 to 12, dd represents Day of the month with range from 01 to 31 and yy is year from 00 to 99.