在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#官方文档或其他资源来了解更多相关的用法和功能。