且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何转换"时间"从日期时间转换成int?

更新时间:2023-02-03 15:32:46

第一步是字符串转换为DateTime。使用DateTime.TryParse(字符串值,日期时间出来DT)。然后,当马修·沃森建议正确,得到变量DT的值转换为使用dt.TimeOfDay.TotalMilliseconds毫秒。另外,也可以向跨度转换在TotalSeconds或TotalMinutes它是否适合您的要求。
,尽量避免直接检查如果单元格值为null之前调用ToString()方法。如果我想避免的检查,我会做的编译器做使用类似:行[3] .Cells [2] .value的+,而不是Value.ToString()

First step is to convert string to DateTime. Use DateTime.TryParse(string value, out DateTime dt). Then as Mathew Watson rightly suggested, get the value of variable dt converted to milliseconds using dt.TimeOfDay.TotalMilliseconds. It is also possible to convert the span in TotalSeconds or TotalMinutes if it suits your requirement. Try to avoid calling ToString() method directly before checking if cell value is null. If I want to avoid the check, I would make compiler to do it by using something like : Rows[3].Cells[2].Value + "" instead of Value.ToString().