DateTime in C#


In this article we perform various operations on dates and times using DateTime. A DateTime structure represents dates and times. We use DateTime to work with dates, times, and both. The DateTime type in the C# language provides useful methods and properties for computing these values. DateTime represents a date and time of the day.  For example there are methods to find important days, such as yesterday, tomorrow, the first of the year, and the last day.
1. To find today's date

Console.WriteLine("Today: {0}", DateTime.Today);

Console.WriteLine("Today: {0}", DateTime.Now);

Difference between Today and Now property

Today - This displays today's date. The time value is 12:00:00.

Now - This displays the current date and time of the system, expressed as the local time. In other words the Now property returns a DateTime object that has the date and time values for right now.
2.  To find yesterday's date

class Program

    {

        static DateTime Yesterdaydate()

        {

            return DateTime.Today.AddDays(-1);

        }

 

        static void Main(string[] args)

        {

            DateTime yes = Yesterdaydate();

            Console.WriteLine("Yesterday was: {0}", yes);

        }

    }

 

3. To find tomorrow's date

 

class Program

    {

        static DateTime Yesterdaydate()

        {

            return DateTime.Today.AddDays(1);

        }

 

static void Main(string[] args)

 

        {      

           DateTime d = Tomorrowdate();

           Console.WriteLine("Tomorrow date was: {0}", d);

        }
4.  To find the First day in a year

class Program

    {

        static DateTime FirstDayOfYear()

        {

            return FirstDayOfYear(DateTime.Today);

        }

        static DateTime FirstDayOfYear(DateTime y)

        {

            return new DateTime(y.Year, 1, 1);

        }

        static void Main(string[] args)

        {

          DateTime m = new DateTime(1999, 6, 1);

           Console.WriteLine("First day of 1999: {0}",

           FirstDayOfYear(m));

        }

 
5. Adding DateTime values

DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");

           TimeSpan addduration = TimeSpan.Parse("1.01:01:01");

           theDate = theDate.Add(addduration);
6.  Subtracting DateTime values

DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");

           TimeSpan addduration = TimeSpan.Parse("1.01:01:01");

           theDate = theDate.Subtract(addduration);

           Console.WriteLine("abstract date is: {0}", theDate);

 
Other DateTime Properties
7. DayOfWeek
DayOfWeek property returns the name of the day in a week.

DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);

Console.WriteLine("Day of Week:{0}", findwyd.DayOfWeek);

Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);

Console.WriteLine("Time of Day:{0}", findwyd.TimeOfDay);
8. DayOfYear
The DayOfYear property returns the day of a year.

DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);

Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);
TimeOfDay
The TimeOfDay property returns the time element from a DateTime.

DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);

Console.WriteLine("Time of Day:{0}", findwyd.TimeOfDay);
For Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace datetime_function

{

    class Program

    {

        static DateTime Yesterdaydate()

        {

            return DateTime.Today.AddDays(-1);

        }

 

        static DateTime Tomorrowdate()

        {

            return DateTime.Today.AddDays(1);

        }

        static DateTime FirstDayOfYear()

        {

            return FirstDayOfYear(DateTime.Today);

        }

        static DateTime FirstDayOfYear(DateTime y)

        {

            return new DateTime(y.Year, 1, 1);

        }

        static void Main(string[] args)

 

        {

            DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);

            Console.WriteLine("Today: {0}", DateTime.Today);

            Console.WriteLine("Today: {0}", DateTime.Now);

           DateTime yes = Yesterdaydate();

           Console.WriteLine("Yesterday was: {0}", yes);

           DateTime d = Tomorrowdate();

           Console.WriteLine("Tomorrow date was: {0}", d);

           DateTime m = new DateTime(1999, 6, 1);

           Console.WriteLine("First day of 1999: {0}",

           FirstDayOfYear(m));

           DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");

           TimeSpan addduration = TimeSpan.Parse("1.01:01:01");

           theDate = theDate.Add(addduration);

           Console.WriteLine("add date is: {0}",theDate);

           theDate = theDate.Subtract(addduration);

           Console.WriteLine("abstract date is: {0}", theDate);

           Console.WriteLine("Day of Week:{0}", findwyd.DayOfWeek);

           Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);

           Console.WriteLine("Time of Day:{0}", findwyd.TimeOfDay);

        }

    }

}
OUTPUT
datetime2.gif

Up Next
    Ebook Download
    View all
    Learn
    View all