一、DateTime获取时间,DateTime转成字符串格式
1.C#时间字符串格式化
yyyy-MM-dd HH:mm:ss:ms
//Vue
//bootstrap date
- Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"));
- Console.WriteLine(DateTime.Today);
2.C#DateTime 获取时间
- DateTime now = DateTime.Now;
- DateTime date1 = new DateTime(now.Year, 1, 1);
- Console.WriteLine(date1);
-
- for (int i = 1; i < 13; i++)
- {
- // Console.WriteLine(now.AddDays(-i));
- // Console.WriteLine(now.AddMonths(i));
- }
-
-
- Console.WriteLine(now.Year);//本年
- Console.WriteLine(now.Hour);
- Console.WriteLine(now.Day);
- Console.WriteLine(now.DayOfWeek); //周几
- Console.WriteLine(now.DayOfYear);//今年的第几天
二、字符串转换成DateTime类型
- Console.WriteLine(Convert.ToDateTime("12:30"));
- Console.WriteLine(Convert.ToDateTime("2020-1-30"));
- Console.WriteLine(Convert.ToDateTime("2020/1/30"));
-
-
-
- DateTime date3 = Convert.ToDateTime("2020/1/30");
三、TimeSpan时间段获取时间差
- TimeSpan span = now - date3;
- Console.WriteLine(span.TotalSeconds);
- Console.WriteLine(span.TotalMilliseconds);
- Console.WriteLine(span.TotalDays);
- Console.WriteLine(span.TotalHours);
-
- Console.WriteLine(span.Days);
- Console.WriteLine(span.Hours);
四、C#时间点字符串转换
C#时间点字符串转换为日期,当天时间点判断_天马3798的博客-CSDN博客
五、其他: