C# DateTime工具类DateTimeUtil 作者:马育民 • 2024-09-08 18:03 • 阅读:10014 ``` public class DateTimeUtil { public static DateTime parse(string year,string month,string day,string hour,string minute,string second) { int yearInt = Convert.ToInt32(year); int monthInt = Convert.ToInt32(month); int dayInt = Convert.ToInt32(day); int hourInt = Convert.ToInt32(hour); int minuteInt = Convert.ToInt32(minute); int secondInt = Convert.ToInt32(second); return new DateTime(yearInt, monthInt,dayInt,hourInt,minuteInt,secondInt); } public static DateTime parse(string hour, string minute, string second) { DateTime now = DateTime.Now; int hourInt = Convert.ToInt32(hour); int minuteInt = Convert.ToInt32(minute); int secondInt = Convert.ToInt32(second); return new DateTime(now.Year, now.Month, now.Day, hourInt, minuteInt, secondInt); } public static DateTime parse(int hour, int minute, int second) { DateTime now = DateTime.Now; return new DateTime(now.Year, now.Month, now.Day, hour, minute, second); } /// /// 传入时间,格式 HH:mm:ss /// /// /// public static DateTime parse(string time) { string[] timeArr = time.Split(':'); DateTime now = DateTime.Now; int hourInt = Convert.ToInt32(timeArr[0]); int minuteInt = Convert.ToInt32(timeArr[1]); int secondInt = Convert.ToInt32(timeArr[2]); return new DateTime(now.Year, now.Month, now.Day, hourInt, minuteInt, secondInt); } } ``` 原文出处:https://malaoshi.top/show_1IX8OEn1Mblv.html