Click here to Skip to main content
15,889,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

VB
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
VB
Public Property Date As Nullable(Of DateTime)
	Get
	Set


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.

VB
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
Posted
Updated 3-Nov-16 3:30am
v2

1 solution

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:
VB
Dim dateWithTime As DateTime = DateTime.Now '' 2016/11/03 12:30
Dim dateWithoutTime As DateTime = dateWithTime.Date '' 2016/11/03

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

[Edited]
This may work:
VB
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


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.
 
Share this answer
 
v3
Comments
Member 11403304 3-Nov-16 9:06am    
Thanks for the help. However I am not sure how to extract the date part from objCurrentWarrantStatus. I do not have a lot of vb.net coding experience.
phil.o 3-Nov-16 9:09am    
Since I do not have the code of the sc.Integration.Mncis.Library.v4.WarrantStatus.GetCurrent() method which assigns your variable a value, I cannot say anything else, sorry. I am not even sure this variable is a DateTime structure. Are you storing a DateTime as a string in the database?
Member 11403304 3-Nov-16 9:22am    
Here is the WarrantStatus Class.
public static WarrantStatus GetCurrent(string astrWarrantNumber, bool ablnUsePrimaryDatastoreOnly)

And for the date
Public Property Date As Nullable(Of DateTime)
Get
Set

phil.o 3-Nov-16 9:26am    
I cannot see any datetime, sorry. How do you retrieve that datetime from the database? Where do you store it and how?
Please avoid posting code in comments. Rather use the green "Improve question" button and qualify your question with more relevant code snippets.
Member 11403304 3-Nov-16 9:28am    
Okay I will add the code in the question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900