DEV Community

firdavs090
firdavs090

Posted on

C# {DateTime}

Current Date and Time:

Now: Gets the current date and time.

DateTime now = DateTime.Now;
Enter fullscreen mode Exit fullscreen mode

UtcNow: Gets the current date and time in Coordinated Universal Time (UTC).

DateTime utcNow = DateTime.UtcNow;
Enter fullscreen mode Exit fullscreen mode

Specific Date and Time:

Constructor: Creates a specific date and time.

DateTime specificDate = new DateTime(2024, 7, 31, 14, 30, 0);
Enter fullscreen mode Exit fullscreen mode

Today: Gets the current date with the time component set to 00:00:00.

DateTime today = DateTime.Today;
Enter fullscreen mode Exit fullscreen mode

Parsing:

Parse: Converts the string representation of a date and time to its 'DateTime' equivalent.
Enter fullscreen mode Exit fullscreen mode
DateTime parsedDate = DateTime.Parse("2024-07-31");
Enter fullscreen mode Exit fullscreen mode

TryParse: Tries to convert the string representation of a date and time to its 'DateTime' equivalent and returns a boolean indicating success or failure.

DateTime result;
bool success = DateTime.TryParse("2024-07-31", out result);
Enter fullscreen mode Exit fullscreen mode

DateTime Properties:

Year, Month, Day: Gets the year, month, and day components of the date.
Enter fullscreen mode Exit fullscreen mode
int year = now.Year;
int month = now.Month;
int day = now.Day;
Enter fullscreen mode Exit fullscreen mode

Hour, Minute, Second: Gets the hour, minute, and second components of the time.

int hour = now.Hour;
int minute = now.Minute;
int second = now.Second;
Enter fullscreen mode Exit fullscreen mode

DayOfWeek: Gets the day of the week.

DayOfWeek dayOfWeek = now.DayOfWeek;
Enter fullscreen mode Exit fullscreen mode

DayOfYear: Gets the day of the year.

int dayOfYear = now.DayOfYear;
Enter fullscreen mode Exit fullscreen mode

DateTime Methods:
Adding and Subtracting:

AddDays, AddMonths, AddYears: Adds the specified number of days, months, or years to the date.
Enter fullscreen mode Exit fullscreen mode
DateTime nextWeek = now.AddDays(7);
DateTime nextMonth = now.AddMonths(1);
DateTime nextYear = now.AddYears(1);
Enter fullscreen mode Exit fullscreen mode

AddHours, AddMinutes, AddSeconds: Adds the specified number of hours, minutes, or seconds to the time.

DateTime inTwoHours = now.AddHours(2);
DateTime inThirtyMinutes = now.AddMinutes(30);
DateTime inTenSeconds = now.AddSeconds(10);
Enter fullscreen mode Exit fullscreen mode

Subtract:

Subtract: Subtracts a specified time interval from the date.
Enter fullscreen mode Exit fullscreen mode
TimeSpan duration = new TimeSpan(1, 0, 0, 0); // 1 day
DateTime yesterday = now.Subtract(duration);
Enter fullscreen mode Exit fullscreen mode

Comparison:

CompareTo: Compares two DateTime instances.
Enter fullscreen mode Exit fullscreen mode
int comparison = now.CompareTo(specificDate);
Enter fullscreen mode Exit fullscreen mode

Equals: Checks if two DateTime instances are equal.

bool isEqual = now.Equals(specificDate);
Enter fullscreen mode Exit fullscreen mode

Before, After: Checks if one date is before or after another date.

bool isBefore = specificDate < now;
bool isAfter = specificDate > now;
Enter fullscreen mode Exit fullscreen mode

Formatting DateTime:

ToString: Converts the DateTime to its string representation.
Enter fullscreen mode Exit fullscreen mode
string dateString = now.ToString();
Enter fullscreen mode Exit fullscreen mode

ToString with format: Converts the DateTime to its string representation with a specified format.

string formattedDate = now.ToString("yyyy-MM-dd HH:mm:ss");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)