如何在没有时间的情况下比较日期和时间? [英] How do I compare date with time to date without time?

查看:110
本文介绍了如何在没有时间的情况下比较日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用对象objCurrentWarrantStatus来获取数据库日期时间,即#11/3/2016 6:36:49 AM#。我想将它与我在xml文档中的日期进行比较,< warrantstatusdate> 2016-11-03

Xml日期没有时间,而数据库日期有时间在里面。

我的代码失败的原因是这两者的类型不同。比较#11/3/2016 6:36:49 AM#到2016-11-03不起作用。

我下面的代码应该通过(不生成错误),但由于数据库日期也有时间,我无法将它与xml文档中的日期进行比较,因为xml文档日期没有时间。这使我的代码失败。如果我能弄清楚要改变什么,那么这两个日期在同一个格式中,那么我的代码就不会失败。

我需要更改什么才能使其中有时间的数据库日期和没有时间的xml日期采用相同的格式进行比较?



I am using an object objCurrentWarrantStatus to get the database datetime which is #11/3/2016 6:36:49 AM#. I would like to compare it with the date I have in xml document which is <warrantstatusdate>2016-11-03
Xml date does not have time while database date has time in it.
My code is failing for the reason that these two are not of the same type. Comparing #11/3/2016 6:36:49 AM# to 2016-11-03 is not working.
The code I have below is supposed to pass (not generate an error) but since the database date also have time in it I am not able to compare it to the date in xml document because xml document date has no time in it. This is making my code to fail. If I can figure out what to change so these two dates are in the same formate, then my code will not fail.
What do I need to change so that the database date which has time in it and the xml date which has no time in it are in the same format to compare them?

objCurrentWarrantStatus = Msc.Integration.Mncis.Library.v4.WarrantStatus.GetCurrent(Trim(objXMLInputDoc.DocumentElement.SelectSingleNode("msc:WarrantNumber/msc:ID", objXMLNameSpaceManager).InnerText))
        If (Not objCurrentWarrantStatus Is Nothing) _
            AndAlso ((objXMLInputDoc.DocumentElement.SelectSingleNode("msc:CurrentWarrantStatus/msc:WarrantStatusTypeText/@code", objXMLNameSpaceManager).InnerText <> objCurrentWarrantStatus.TypeCodeWord) _
            Or (CDate(objXMLInputDoc.DocumentElement.SelectSingleNode("msc:CurrentWarrantStatus/msc:WarrantStatusDate", objXMLNameSpaceManager).InnerText) <> objCurrentWarrantStatus.Date)) Then
            strErrorResponse = "The date used for CurrentWarrantStatus was incorrect."
            objXMLInputDoc.DocumentElement.SetAttribute("error", strErrorResponse)
        End If





这是该日期的财产



Here is the property for the date

Public Property Date As Nullable(Of DateTime)
	Get
	Set





我尝试过:



我试过以下代码但它失败了,因为数据库中的日期(objCurrentWarrantStatus)有时间,而xml文档中的日期没有时间。所以这些都无法比较。





What I have tried:

I have tried the following code but it is failing because the date in the database (objCurrentWarrantStatus) has time in it while the date in xml document does not have time. So these cannot be compared.

objCurrentWarrantStatus = Msc.Integration.Mncis.Library.v4.WarrantStatus.GetCurrent(Trim(objXMLInputDoc.DocumentElement.SelectSingleNode("msc:WarrantNumber/msc:ID", objXMLNameSpaceManager).InnerText))
        If (Not objCurrentWarrantStatus Is Nothing) _
            AndAlso ((objXMLInputDoc.DocumentElement.SelectSingleNode("msc:CurrentWarrantStatus/msc:WarrantStatusTypeText/@code", objXMLNameSpaceManager).InnerText <> objCurrentWarrantStatus.TypeCodeWord) _
            Or (CDate(objXMLInputDoc.DocumentElement.SelectSingleNode("msc:CurrentWarrantStatus/msc:WarrantStatusDate", objXMLNameSpaceManager).InnerText) <> objCurrentWarrantStatus.Date)) Then
            strErrorResponse = "The date used for CurrentWarrantStatus was incorrect."
            objXMLInputDoc.DocumentElement.SetAttribute("error", strErrorResponse)
        End If

推荐答案

根据MSDN:日期时间结构 [ ^ ],有一个 DateTime.Date 属性,您可以使用该属性从中提取日期时间涉及日期的部分。

类似于:

According to MSDN: DateTime Structure[^], there is a DateTime.Date property which you can use to extract from a datetime the part which concerns the date.
Something like:
Dim dateWithTime As DateTime = DateTime.Now '' 2016/11/03 12:30
Dim dateWithoutTime As DateTime = dateWithTime.Date '' 2016/11/03



因此,您应该从 objCurrentWarrantStatus 中提取日期部分,然后再将其与XML中的日期部分进行比较文件。





这可能有效:


So, you should extract the date part from objCurrentWarrantStatus before comparing it to the one in the XML document.


This may work:

Dim dateWithoutTime As DateTime
If (objCurrentWarrantStatus.Date.HasValue)
   dateWithoutTime = objCurrentWarrantStatus.Date.Value.Date
Else
   '' No datetime value. Something should be decided about the dateWithoutTime variable.
End If





或者更好:如果你只想要Date属性中的日期部分(看似合乎逻辑),那么当你从中检索数据时,最好这样做数据库。

[/已编辑]



亲切。



Or better: if you only want the date part in the Date property (which would seem logical), better do that when you retrieve the data from the database.
[/Edited]

Kindly.


这篇关于如何在没有时间的情况下比较日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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