C#中DataTime的多种用法

1、DateTime.Now.ToString();获取当前时间

DateTime.Now.ToString():返回值:2023-1-2 20:02:10

2、DateTime.Now.ToLongDateString().ToString();

DateTime.Now.ToLongDateString().ToString():返回值:202312

3、DateTime.Now.ToShortDateString().ToString();

DateTime.Now.ToShortDateString().ToString():返回值:2023-1-2

4、DateTime.Now.Date.ToString():

DateTime.Now.Date.ToString():返回值:2023-1-2 0:00:00

5、DateTime.Now.ToLongTimeString().ToString():

DateTime.Now.ToLongTimeString().ToString():返回值:20:02:10

6、DateTime.Now.ToShortTimeString().ToString():

DateTime.Now.ToShortTimeString().ToString():返回值:20:16

7、DateTime.Now.ToString(“hh:mm:ss”):

DateTime.Now.ToString("hh:mm:ss"):返回值:08:05:57

8、DateTime.Now.TimeOfDay.ToString():

DateTime.Now.TimeOfDay.ToString():返回值:20:33:50.2787500

9、DateTime.ToFileTime().ToString():转为windows文件时间

DateTime.ToFileTime().ToString():返回值:128650040212500000

10、DateTime.Now.Year.ToString();

DateTime.Now.Year.ToString(); 返回值:2023

11、添加时间:

//添加一年
DateTime.Now.AddYears(1).ToString();

//添加一天
DateTime.Now.AddDays(1).ToString();

12、计算一年中的第几周

GregorianCalendar gc = new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
var s1 = weekOfYear.ToString();

你可能感兴趣的:(c#,开发语言,java)