在C#中,可以使用DateTime類來處理日期和時間。下面是一些常見的DateTime的用法:
DateTime now = DateTime.Now;
DateTime date = new DateTime(2021, 9, 30);
DateTime dateTime = new DateTime(2021, 9, 30, 10, 30, 0);
int year = now.Year;
int month = now.Month;
int day = now.Day;
int hour = now.Hour;
int minute = now.Minute;
int second = now.Second;
string formattedDate = now.ToString("yyyy-MM-dd");
string formattedDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
DateTime tomorrow = now.AddDays(1);
DateTime nextWeek = now.AddDays(7);
DateTime oneHourLater = now.AddHours(1);
DateTime oneMinuteAgo = now.AddMinutes(-1);
bool isSameDate = now.Date == date.Date;
bool isBefore = now < dateTime;
bool isAfter = now > dateTime;
這只是一些DateTime類的基本用法,你還可以通過查閱C#官方文檔或其他資源來了解更多相關的用法和功能。