• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Date time syntax

I am checking for a credit card related clause in vb.net code .Not sure how to check that,looking for syntax

'If  the credit card is there for 6 months more than the duedate in that table I need to remove the entry.'
I have a business object which holds all thefields for that table like (Card Expiration date,card create datetime)
1 Solution
 TimeSpan span = System.DateTime.Now.Subtract (cardcreatedate);

if (span.Days > 180)
//Call stored proc to remove data.
// I am assuming that cardcreatedate is the datetime that the entry for the card was made in the table.

Open in new window


You can use AddMonths to validate it, I am going to assume that when you said "CREDIT CARD IS THER FOR 6 MORE THAN DUEDATE" you mean Credit Card Expiration Date, and that both fields are Date Type, so it will be something like this

If CreditCardExpirationDateField >= DueDate.AddMonths(6) Then
   'Remove the Entry
End if
Try this:

Dim ccIsThereDate As DateTime = #7/1/2010#
Dim dueDate As DateTime = #1/1/2011#

Console.WriteLine("Days {0}", (dueDate - ccIsThereDate).Days)

If (dueDate - ccIsThereDate).Days > 180 Then
   Console.WriteLine("The credit card is there for 6 months more than the duedate.")
End If

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

käµfm³d 👽Commented:
For rawinnlnx9 or wdosanjos' example, you would want to use TotalDays as opposed to Days.
@kaufmed, I think TotalDays would make sense if the calculation involves date and time.  If both variables represent just dates, Days should have the same effect.
käµfm³d 👽Commented:

On second thought, I agree. It should have the same effect. I'm so used to working with the seconds/milliseconds members of TimeSpan that I just instinctively applied it here. After reviewing the documentation again, I see that, for the purposes of this question, Days and TotalDays would be equivalent.

The one "flaw" with this logic, though, is that there is an assumption of a flat 180 days equals six months. I think gamarrojgq's approach might be more appropriate here.
@kaufmed, I agree with your analysis.  @gamarrojgq approach is more flexible as it can address either requirements properly (180 days or 6 months).

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now