C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# DateTime Class

In C#, the DateTime struct is used to represent dates and times. It is part of the System namespace. In this tutorial, we'll cover the basics of the DateTime class, including creating, formatting, and manipulating DateTime objects.

  • Creating DateTime objects:

To create a DateTime object, you can use the constructor or the static properties and methods provided by the class.

  • Current date and time:

    DateTime now = DateTime.Now;
    
  • Current date (time set to midnight):

    DateTime today = DateTime.Today;
    
  • Specific date and time:

    DateTime customDate = new DateTime(2023, 4, 27, 14, 30, 0);
    
  • Formatting DateTime objects:

You can format a DateTime object as a string using the ToString() method and format specifiers.

  • Default format:

    string defaultFormat = now.ToString(); // e.g., "04/27/2023 14:30:00"
    
  • Custom format:

    string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss"); // e.g., "2023-04-27 14:30:00"
    
  • Manipulating DateTime objects:

The DateTime class provides methods and properties to manipulate dates and times.

  • Add or subtract time:

    DateTime oneHourLater = now.AddHours(1);
    DateTime oneDayEarlier = now.AddDays(-1);
    
  • Access date and time components:

    int year = now.Year;
    int month = now.Month;
    int day = now.Day;
    int hour = now.Hour;
    int minute = now.Minute;
    int second = now.Second;
    DayOfWeek dayOfWeek = now.DayOfWeek;
    
  • Comparing DateTime objects:

You can compare DateTime objects using the standard comparison operators, such as <, >, ==, and !=.

DateTime date1 = new DateTime(2023, 4, 27);
DateTime date2 = new DateTime(2023, 4, 28);

bool areEqual = date1 == date2; // false
bool isBefore = date1 < date2; // true
  • Parsing DateTime objects from strings:

You can create a DateTime object from a string using the Parse() and TryParse() methods.

  • Parse (throws an exception if the input is not a valid date):

    DateTime parsedDate = DateTime.Parse("2023-04-27");
    
  • TryParse (returns a boolean indicating whether the parsing was successful):

    DateTime parsedDate;
    bool success = DateTime.TryParse("2023-04-27", out parsedDate);
    
  1. Working with DateTime in C#:

    • Description: The DateTime class is commonly used for representing and manipulating date and time values.
    • Code:
      DateTime currentDateTime = DateTime.Now;
      
  2. Creating DateTime objects in C#:

    • Description: Creating DateTime objects with specific date and time values.
    • Code:
      DateTime specificDateTime = new DateTime(2023, 12, 31, 15, 30, 0);
      
  3. DateTime formatting in C#:

    • Description: Formatting DateTime values as strings using custom format strings.
    • Code:
      string formattedDate = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
      
  4. Parsing DateTime strings in C#:

    • Description: Parsing strings to convert them into DateTime objects.
    • Code:
      string dateString = "2023-12-31";
      DateTime parsedDate = DateTime.Parse(dateString);
      
  5. Adding and subtracting time with DateTime in C#:

    • Description: Performing arithmetic operations to add or subtract time from DateTime objects.
    • Code:
      DateTime futureDate = currentDateTime.AddMonths(3);
      DateTime pastDate = currentDateTime.AddDays(-7);
      
  6. DateTime comparison in C#:

    • Description: Comparing DateTime values to determine their order.
    • Code:
      DateTime earlierDate = new DateTime(2023, 1, 1);
      bool isLater = currentDateTime > earlierDate;
      
  7. DateTime manipulation in C#:

    • Description: Manipulating DateTime values using methods like AddDays, AddMonths, and more.
    • Code:
      DateTime newDate = currentDateTime.AddDays(5).AddMonths(2);
      
  8. Handling time zones with DateTime in C#:

    • Description: Working with time zones to ensure accurate representation and manipulation of date and time values.
    • Code:
      TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
      DateTime utcDateTime = DateTime.UtcNow;
      DateTime convertedDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZone);