以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]时间计算经常报错,怎么避免  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=145172)

--  作者:wldhj
--  发布时间:2020/1/10 16:31:00
--  [求助]时间计算经常报错,怎么避免
Dim time As String = e.PostValues("time")
Dim t As Integer=(Date.Now - CDate(time)).TotalSeconds

这个e.PostValues("time")也是通过 Date.Now生成传到前端,再通过jaxa传过来的

大部分运行正常,偶尔报
Exception has been thrown by the target of an invocation.
Conversion from string "" to type \'Date\' is not valid.
怎么避免这种情况?

--  作者:有点蓝
--  发布时间:2020/1/10 16:39:00
--  
这种提示就是前端没有传值回来导致的,也就是e.PostValues("time")没有值,比较严谨的用法应该使用TryParse转换:http://www.foxtable.com/webhelp/topics/0324.htm

Dim time As String = e.PostValues("time")
dim d as date
if date.TryParse(time,d) then
Dim t As Integer=(Date.Now - d).TotalSeconds
else
msgbox(“出错,非法时间格式”)
end if