查找日期是否介于2个日期之间? [英] Find if date falls between 2 dates??

查看:96
本文介绍了查找日期是否介于2个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一些代码会这样做吗?


Dave

解决方案

Dave,


Dim date1 As DateTime =" 12/1/2005"

Dim date2 As DateTime =" 12/31/2005"

Dim myDate As DateTime


myDate = InputBox(" Date?")


if myDate> = date1和myDate< = date2然后

MsgBox(" Between")

Else

MsgBox(Not between)

结束如果


Kerry Moorman

" df ******** @ hotmail.com"写道:

任何人都有一些代码可以做到这一点?

Dave



戴夫,


在我看来,最简单的方法是检查使用Ticks属性

http://msdn.microsoft.com/ library / de ... tickstopic.asp


我希望这会有所帮助,


Cor

戴夫,

我会使用范围模式:
http://www.martinfowler.com/ap2/range.html


Dim december2005 As New Range(Of DateTime) )(#12/1/2005#,#12/31/2005#)

如果是12月2005.Contains(DateTime.Now)那么

''做一些令人兴奋的事情,因为十二月!

结束如果

我有一个定义的范围(Of T)(可在VS 2005中使用):
http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx

我定义了一个TimeRange(可用于VS 2002,2003& 2005):
http://www.tsbradley.net /Cookbook/Pa...timeRange.aspx

如果你想要一个普通的话。可以在VS 2002,2003或2005中使用的DateRange,你

可以使用类似的东西:


''

''版权? 2005年,Jay B. Harlow,保留所有权利。

''

期权严格上限

期权明确的

Public Structure DateRange


Private ReadOnly m_start As DateTime

Private ReadOnly m_end As DateTime


Public Sub New(ByVal start as DateTime,ByVal [end] As DateTime)

m_start = start.Date

m_end = [end] .Date

End Sub


Public ReadOnly Property Start()As DateTime

获取

返回m_start

结束获取

结束物业


Public ReadOnly Property [End]()As DateTime

获取

返回m_end

结束获取

结束物业


Public ReadOnly Property IsEmpty()As Boolean

获取

返回m_start.CompareTo(Nothing)= 0并且还要

m_end.CompareTo(Nothing)= 0

返回m_start.Equals(没什么)AndAlso m_end.Equals(没什么)

结束获取

En d属性


公共函数包含(ByVal值为DateTime)为布尔值

value = value.Date

返回m_start.CompareTo (值)< = 0并且还值。比较(m_end)

< = 0

结束功能


公共功能包含(ByVal值As DateRange)As Boolean

返回Me.Contains(value.m_start)AndAlso Me.Contains(value.m_end)

结束函数


公共函数重叠(ByVal值为DateRange)As Boolean

返回Me.Contains(value)OrElse value.Contains(m_start)OrElse

value .Contains(m_end)

结束函数


公共覆盖函数GetHashCode()作为整数

返回m_start.GetHashCode()Xor m_end.GetHashCode()

结束函数


公共重载覆盖函数等于(ByVal obj As Object)As

Boolean

如果TypeOf obj是DateRange那么

返回等于(DirectCast(obj,DateRange))

其他

返回错误

结束如果

结束功能


公共超载函数Equals(ByVal other As DateRange)As Boolean

返回m_start.Equals(other.m_start)AndAlso

m_end.Equals(other.m_end)

结束功能


结束结构


注意:范围(日期时间)将包括日期和时间。一个

DateTime的时间值,上面的DateRange只考虑DateTime的Date部分。

虽然上面的TimeRange只考虑DateTime的Time部分。 />

-

希望这有帮助

Jay [MVP - Outlook]

..NET应用程序架构师,爱好者,&福音传教士

T.S.布拉德利 - http://www.tsbradley.net

< ; DF ******** @ hotmail.com>在消息中写道

新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...

|任何人都有一些代码会这样做吗?

|

|戴夫

|


Anyone have some code that will do this?

Dave

解决方案

Dave,

Dim date1 As DateTime = "12/1/2005"
Dim date2 As DateTime = "12/31/2005"
Dim myDate As DateTime

myDate = InputBox("Date?")

If myDate >= date1 And myDate <= date2 Then
MsgBox("Between")
Else
MsgBox("Not between")
End If

Kerry Moorman
"df********@hotmail.com" wrote:

Anyone have some code that will do this?

Dave



Dave,

In my opinion is the easiest way to check that using the Ticks property

http://msdn.microsoft.com/library/de...tickstopic.asp

I hope this helps,

Cor


Dave,
I would use a Range Pattern:
http://www.martinfowler.com/ap2/range.html

Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)
If december2005.Contains(DateTime.Now) Then
'' do something exciting because its December!
End If
I have a Range(Of T) defined at (usable in VS 2005):
http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005, you
can use something like:

''
'' Copyright ? 2005, Jay B. Harlow, All Rights Reserved.
''
Option Strict On
Option Explicit On

Public Structure DateRange

Private ReadOnly m_start As DateTime
Private ReadOnly m_end As DateTime

Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
m_start = start.Date
m_end = [end].Date
End Sub

Public ReadOnly Property Start() As DateTime
Get
Return m_start
End Get
End Property

Public ReadOnly Property [End]() As DateTime
Get
Return m_end
End Get
End Property

Public ReadOnly Property IsEmpty() As Boolean
Get
Return m_start.CompareTo(Nothing) = 0 AndAlso
m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property

Public Function Contains(ByVal value As DateTime) As Boolean
value = value.Date
Return m_start.CompareTo(value) <= 0 AndAlso value.CompareTo(m_end)
<= 0
End Function

Public Function Contains(ByVal value As DateRange) As Boolean
Return Me.Contains(value.m_start) AndAlso Me.Contains(value.m_end)
End Function

Public Function Overlaps(ByVal value As DateRange) As Boolean
Return Me.Contains(value) OrElse value.Contains(m_start) OrElse
value.Contains(m_end)
End Function

Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(obj, DateRange))
Else
Return False
End If
End Function

Public Overloads Function Equals(ByVal other As DateRange) As Boolean
Return m_start.Equals(other.m_start) AndAlso
m_end.Equals(other.m_end)
End Function

End Structure

NOTE: Range(Of DateTime) will include both the Date & the Time values of a
DateTime, the above DateRange only considers the Date part of a DateTime.
While the above TimeRange only consider the Time part of a DateTime.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<df********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
| Anyone have some code that will do this?
|
| Dave
|


这篇关于查找日期是否介于2个日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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