如果两个日期之间的条件 [英] If Condition between two dates

查看:95
本文介绍了如果两个日期之间的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IF条件下遇到问题。



请查看我的代码



  Dim  d from  As  DateTime = Trim(dt.Rows(n  -   1 )。单元格( 2 )。值)'  sample 07/08/2013  
Dim dto As DateTime = Trim(dt.Rows(n - 1 )。单元格( 3 )。值) ' 示例07/15/2013
Dim dnow As DateTime = 日期 .Now.ToString( MM / dd / yyyy

如果 dnow> = dfrom AndAlso dnow< = dto 然后
MsgBox( 其工作
否则
MsgBox( 史诗失败!!!

结束 如果





我总是得到msgboxEpic Fail.Lol

解决方案

I怀疑如果您提供的代码编译没有错误。无论如何,尝试使用DateTime.Parse如下:



 Dim dfrom As DateTime = DateTime.Parse (修剪(dt.Rows(n  -  1).Cells(2).Value))





dnow 不需要转换为字符串 - 您可以在DateTime对象本身进行比较。之后应该给出正确的结果。


1.从中转换单元格 dt as DateTime 使用CDATE的数据类型()

2.假设两个单元格在 dt 只包含mm / dd / yyyy并且不包含任何时间数据。

3.仅保存当前日期时间的日期部分 dnow 变量使用 .Date 属性否则比较e等于将失败。



  Dim  d from  As  DateTime =  CDATE (dt.Rows(n  -   1  )。细胞( 2 )。值)' 样本07 / 08/2013  
Dim dto As DateTime = CDATE (dt.Rows(n - 1 )。单元格( 3 )。值)' 示例07/15/2013
Dim dnow As DateTime = 日期。现在。日期 ' 仅使用当前日期时间的日期部分
如果 dnow> = dfrom AndAlso dnow< = dto 然后
MsgBox( 其工作
否则
MsgBox( Epic Fail !!!
结束 如果


Having problem in IF condition.

Please review my code

 Dim dfrom As DateTime = Trim(dt.Rows(n - 1).Cells(2).Value) ' sample 07/08/2013
 Dim dto As DateTime = Trim(dt.Rows(n - 1).Cells(3).Value) 'sample 07/15/2013
Dim dnow As DateTime = Date.Now.ToString("MM/dd/yyyy")

If dnow >= dfrom AndAlso dnow <= dto Then
    MsgBox("Its working")
Else
    MsgBox(" Epic Fail!!! ")

End If



and i always get msgbox "Epic Fail". Lol

解决方案

I suspect if the code you gave compiles without errors. Anyway, try using DateTime.Parse like following:

Dim dfrom As DateTime = DateTime.Parse(Trim(dt.Rows(n - 1).Cells(2).Value))



and dnow does not need to be converted to string - the comparison you can do in DateTime object itself. Should give correct result after that.


1. Casts cells from dt as DateTime datatype using CDATE()
2. Assumes that the two cells in dt only contain mm/dd/yyyy and do not contain any time data.
3. Saves only the date part of the current datetime in dnow variable using .Date property otherwise the compare for equal will fail.

Dim dfrom As DateTime = CDATE(dt.Rows(n - 1).Cells(2).Value) ' sample 07/08/2013
Dim dto As DateTime = CDATE(dt.Rows(n - 1).Cells(3).Value) 'sample 07/15/2013
Dim dnow As DateTime = Date.Now.Date ' Use only Date part of current DateTime
If dnow >= dfrom AndAlso dnow <= dto Then
    MsgBox("Its working")
Else
    MsgBox(" Epic Fail!!! ")
End If


这篇关于如果两个日期之间的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆