SoFunction
Updated on 2025-04-11

Example of C# method to implement DateTime class

In C#, the operations of date and time are mainly implemented through the    class. DateTime provides a wealth of attributes and methods for processing date and time creation, formatting, comparison and calculation operations. Here are some commonly used date functions and features:

1. Creation date and time

1. Directly specify the date and time:

DateTime now = ; // Get the current date and timeDateTime today = ; // Get the current date (time part is 00:00:00)DateTime specificDate = new DateTime(2025, 2, 26, 14, 30, 0); // Specify the specific date and time

2. Parsing date and time from string:

DateTime parsedDate = ("2025-02-26 14:30:00"); // parse from standard format stringDateTime parsedDateWithFormat = ("26/02/2025 14:30", "dd/MM/yyyy HH:mm", null); // Use custom format to parse

2. Obtain the components of date and time

DateTime provides multiple read-only properties to get the various parts of the date and time:

  • Year  : Get the year.
  • Month  : Get the month (1-12).
  • Day  : Get the date (1-31).
  • Hour  : Get hours (0-23).
  • Minute  : Get minutes (0-59).
  • Second  : Get seconds (0-59).

Example:

DateTime now = ;
($"Year: {}, Month: {}, Day: {}");
($"Hour: {}, Minute: {}, Second: {}");

3. Calculation of date and time

1. Addition and subtraction date and time:

DateTime now = ;
DateTime tomorrow = (1); // Add 1 dayDateTime yesterday = (-1); // 1 day offDateTime nextWeek = (1); // Add 1 week (requires an extension method)DateTime nextHour = (1); // Add 1 hour

2. Calculate the difference between two dates:

DateTime date1 = new DateTime(2025, 2, 26);
DateTime date2 = new DateTime(2025, 3, 1);
TimeSpan difference = date2 - date1; // Return the TimeSpan object($"Days: {}, Hours: {}");

4. Format date and time

1. Standard formatting:

DateTime now = ;
string formattedDate = ("yyyy-MM-dd HH:mm:ss"); // Custom formatstring shortDate = (); // Short date format (such as: 2025/02/26)string longDate = (); // Long date format (such as: February 26, 2025)

2. Custom formatting:

string customFormat = ("dd/MM/yyyy HH:mm:ss tt"); // Custom format (such as: 26/02/2025 14:30:00 PM)

5. Comparison of dates and time

1. Compare two dates:

DateTime date1 = new DateTime(2025, 2, 26);
DateTime date2 = new DateTime(2025, 3, 1);

if (date1 < date2)
{
    ("date1 is earlier than date2");
}
else if (date1 > date2)
{
    ("date1 is later than date2");
}
else
{
    ("date1 is the same as date2");
}

2. Judge date range:

DateTime start = new DateTime(2025, 2, 1);
DateTime end = new DateTime(2025, 2, 28);
DateTime testDate = new DateTime(2025, 2, 15);

if (testDate >= start && testDate <= end)
{
    ("testDate is within the range");
}

6. Other common methods

1. Determine whether it is a leap year:

bool isLeapYear = (2024); // Return true

2. Get the day of the week:

DateTime now = ;
string dayOfWeek = (); // Return to the day of the week (such as Wednesday)

7. Expansion method

C# allows adding custom functions to DateTime through extension methods. For example, add an AddWeeks method:

public static class DateTimeExtensions
{
    public static DateTime AddWeeks(this DateTime date, int weeks)
    {
        return (weeks * 7);
    }
}

// useDateTime now = ;
DateTime nextMonth = (4);

Summarize

It is the core structure for processing dates and time in C#, providing rich functions to meet the needs of most date and time operations. By combining DateTime and TimeSpan, date calculation, formatting and comparison operations can be easily implemented.

This is the end of this article about C#’s method examples of DateTime implementation. For more related C# DateTime content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!