Now |
Gets the current date and time. |
DateTime now = DateTime.Now; Â Â // e.g., "2024-07-28 14:35:00" |
UtcNow |
Gets the current date and time in Coordinated Universal Time (UTC). |
DateTime utcNow = DateTime.UtcNow; Â Â // e.g., "2024-07-28 18:35:00" |
Today |
Gets the current date with the time component set to 00:00:00. |
DateTime today = DateTime.Today; Â Â // e.g., "2024-07-28 00:00:00" |
Date |
Gets the date component of the DateTime instance. |
DateTime date = now.Date; Â Â // e.g., "2024-07-28 00:00:00" |
Day |
Gets the day of the month represented by the DateTime instance. |
int day = now.Day; Â Â // e.g., 28 |
Month |
Gets the month component of the DateTime instance. |
int month = now.Month; Â Â // e.g., 7 |
Year |
Gets the year component of the DateTime instance. |
int year = now.Year; Â Â // e.g., 2024 |
Hour |
Gets the hour component of the DateTime instance. |
int hour = now.Hour; Â Â // e.g., 14 |
Minute |
Gets the minute component of the DateTime instance. |
int minute = now.Minute; Â Â // e.g., 35 |
Second |
Gets the second component of the DateTime instance. |
int second = now.Second; Â Â // e.g., 0 |
DayOfWeek |
Gets the day of the week represented by the DateTime instance. |
DayOfWeek dayOfWeek = now.DayOfWeek; Â Â // e.g., DayOfWeek.Sunday |
DayOfYear |
Gets the day of the year represented by the DateTime instance. |
int dayOfYear = now.DayOfYear; Â Â // e.g., 210 |
AddDays |
Adds the specified number of days to the DateTime instance. |
DateTime futureDate = now.AddDays(5); Â Â // e.g., "2024-08-02 14:35:00" |
AddMonths |
Adds the specified number of months to the DateTime instance. |
DateTime futureDate = now.AddMonths(1); Â Â // e.g., "2024-08-28 14:35:00" |
AddYears |
Adds the specified number of years to the DateTime instance |
DateTime futureDate = now.AddYears(1); Â Â // e.g., "2025-07-28 14:35:00" |
AddHours |
Adds the specified number of hours to the DateTime instance. |
DateTime futureDate = now.AddHours(5); Â Â // e.g., "2024-07-28 19:35:00" |
AddMinutes |
Adds the specified number of minutes to the DateTime instance. |
DateTime futureDate = now.AddMinutes(30); Â Â // e.g., "2024-07-28 15:05:00" |
AddSeconds |
Adds the specified number of seconds to the DateTime instance. |
DateTime futureDate = now.AddSeconds(30); Â Â // e.g., "2024-07-28 14:35:30" |
AddMilliseconds |
Adds the specified number of milliseconds to the DateTime instance. |
DateTime futureDate = now.AddMilliseconds(500); Â Â // e.g., "2024-07-28 14:35:00.500" |
AddTicks |
Adds the specified number of ticks to the DateTime instance. |
DateTime futureDate = now.AddTicks(1000000); Â Â // e.g., "2024-07-28 14:35:00.0001000" |
Subtract |
Subtracts the specified date and time from this instance. |
TimeSpan duration = now.Subtract(pastDate); Â Â // e.g., "2.00:00:00" (2 days) |
ToString |
Converts the value of the DateTime instance to its equivalent string representation. |
string str = now.ToString("yyyy-MM-dd HH:mm:ss"); Â Â // "2024-07-28 14:35:00" |
Parse |
Converts the string representation of a date and time to its DateTime equivalent. |
DateTime dt = DateTime.Parse("2024-07-28 14:35:00"); |
TryParse |
Converts the string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded. |
bool success = DateTime.TryParse("2024-07-28 14:35:00", out DateTime dt); |
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by PotatoCodes