关于日期的比较问题(datediff)

Ice1982 2004-07-15 02:22:43
我想用当前的日期(now),与text1.text中的日期做比较。如果now比text1.text的日期早(即小于)则无事。要是now比text1.text的日期晚(即大于),则报错!请问怎样用datediff这函数实现此功能?
...全文
425 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
helanshan 2004-07-15
  • 打赏
  • 举报
回复
同意(mylzw俺是天空里地一片云)的方法。
楼主调用的日期函数(now)返回的是当前日期和时间,而在text1.text中输入的是日期,两者进行对比肯定会报错。
进行对比前最好对当前时间和对比时间进行格式化。如
format(Now,"yyyy-mm-dd") or format(Now,"yyyy年mm月dd日") ‘返回系统日期
format(Text1.Text,"yyyy-mm-dd") or format(Text1.Text,"yyyy年mm月dd日") ’输入的日期。
熊孩子开学喽 2004-07-15
  • 打赏
  • 举报
回复
日期本来就是一个很混乱的东西,有长格式,有短格式,有年月日,有年日月,有月日年
谁知道“04-03-02”代表的是什么日子吗?
mylzw 2004-07-15
  • 打赏
  • 举报
回复
If format(Now,"yyyy-mm-dd") > format(Text1.Text,"yyyy-mm-dd") Then
MsgBox "OK"
Else
MsgBox "False"
End If

另外,最好使用DtPicKer控件来选择日期
lsftest 2004-07-15
  • 打赏
  • 举报
回复
isdate()中的数据应该是什末类型?2008-1-1的格式好像无效吧
主要是执行 If IsDate(Text1) Then 出现的就是ELSE的状态。所以后面的语句就根本不执行了
=======================
你先运行一下这个代码看结果显示什么?????
Private Sub Form_Click()
Print IsDate("2008-1-1")
End Sub

你不要自己输入代码,原样复制运行试试,我这里是true......
northwolves 2004-07-15
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
MsgBox IIf(Now < CDate(Text1), "将来", "过去")
MsgBox IIf(DateDiff("s", Now, CDate(Text1)) > 0, "将来", "过去")
End Sub

Private Sub Form_Load()
Text1.Text = "2008-1-1"
End Sub
Ice1982 2004-07-15
  • 打赏
  • 举报
回复
主要是执行 If IsDate(Text1) Then 出现的就是ELSE的状态。所以后面的语句就根本不执行了
victorycyz 2004-07-15
  • 打赏
  • 举报
回复

你再试一下:

Private Sub Command1_Click()

If IsDate(Text1) Then
If DateDiff("s", Now(), CDate(Text1)) < 0 Then
MsgBox "error"
Else
MsgBox "OK"
End If
End If

End Sub
Ice1982 2004-07-15
  • 打赏
  • 举报
回复
wxrwan(小飞)安你的方法执行后没有什末变化。
isdate()中的数据应该是什末类型?2008-1-1的格式好像无效吧
lsftest 2004-07-15
  • 打赏
  • 举报
回复
northwolves(狼行天下) 你的cdate()总报错!我的text1中是2008-1-1的格式
=========================================
注意你使用的那两个减号,不要用中文输入法来输入。。。。想验证输入的格式是否正确,用isdate(trim(text1.text))....

另外,上面northwolves(狼行天下)兄给出的代码好像跟楼主的要求刚好相反,试改为:
If DateDiff("s", CDate(Text1),Now) > 0 Then MsgBox "err"
wxrwan 2004-07-15
  • 打赏
  • 举报
回复
northwolves(狼行天下)的已经是DATE类型了.
if Isdate(text1) then
If DateDiff("s", Now, Text1) > 0 Then MsgBox "err"
endif
Ice1982 2004-07-15
  • 打赏
  • 举报
回复
northwolves(狼行天下) 你的cdate()总报错!我的text1中是2008-1-1的格式
northwolves 2004-07-15
  • 打赏
  • 举报
回复
不会吧:
Option Explicit

Private Sub Command1_Click()
'If CDate(Text1.Text) > Now Then MsgBox "err"
If DateDiff("s", Now, CDate(Text1)) > 0 Then MsgBox "err"
End Sub

Private Sub Form_Load()
Text1.Text = #1/1/2008#
End Sub
Ice1982 2004-07-15
  • 打赏
  • 举报
回复
我按照 online(龙卷风V2.0--再战江湖) 的方法做后不论text1中是何值都显示"OK"
按期它的办法,都说DATEDIFF中类型不匹配。
我在TEXT中输入的是2008-1-1,应该没问题吧。
为何还报错呢?
online 2004-07-15
  • 打赏
  • 举报
回复
If Now > Text1.Text Then
MsgBox "OK"
Else
MsgBox "False"
End If

呵呵,简单的好,如果text是标准时间的话
northwolves 2004-07-15
  • 打赏
  • 举报
回复

Private Sub Command1_Click()
'If CDate(Text1.Text) > Now Then MsgBox "err"
If DateDiff("s", Now, CDate(Text1)) > 0 Then MsgBox "err"
End Sub

Private Sub Form_Load()
Text1.Text = Date + 10
'text1.text=date-1
End Sub
anosoft 2004-07-15
  • 打赏
  • 举报
回复
if datediff("d",now,cdate(text1.text))<0 then
报错
else
正确
end if
TechnoFantasy 2004-07-15
  • 打赏
  • 举报
回复
如果你的text1里面输入的是标准格式的日期数据,例如2004-07-15,这样比就可以:

If Now > Text1.Text Then
MsgBox "OK"
Else
MsgBox "False"
End If
victorycyz 2004-07-15
  • 打赏
  • 举报
回复

if isdate(text1) then
if datediff('s',now(),cdate(text1))<0 then msgbox "error"
end if

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧