SIGN UP MEMBER LOGIN:    
ARTICLE

DateTime in C#

Posted by Rohatash Kumar Articles | C# Language June 21, 2011
In this article we perform various operations on dates and times using DateTime.
Reader Level:


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

Login to add your contents and source code to this article
share this article :
post comment
 

How can I store the selected date from a MonthCalendar in a Mysql table with c#

Posted by Kweku Danquah Jul 07, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor