如果两个日期之间的条件

编程入门 行业动态 更新时间:2024-10-26 09:19:54
本文介绍了如果两个日期之间的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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

更多推荐

如果两个日期之间的条件

本文发布于:2023-10-20 08:59:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!