我有一个输入字符串HH:MM:SS,例如15:43:13,
现在我想将它转换为日期时间,但只保留没有日期等的小时/时间
可能吗?
例如
string userInput = 15:43:13;
DateTime userInputTime = Convert.ToDateTime(userInput);
Run Code Online (Sandbox Code Playgroud)
会给我一个完整的日期,包括年份等,有没有办法将它转换为HH:MM:SS没有triming/substring?
谢谢
jga*_*fin 14
正如其他人所说,这是一个TimeSpan.
这样做可以获得日期时间
string userInput = "15:43:13";
var time = TimeSpan.Parse(userInput);
var dateTime = DateTime.Today.Add(time);
Run Code Online (Sandbox Code Playgroud)