Erich Peterson

Bio and Blog

Archive for the ‘.Net General’ Category

Calculating Next Invoice Date

Have you have every needed to calculate the date of the next invoice for a customer given their frequency of payment (e.g. monthly or yearly), the day of the month the first invoice was sent out (e.g. 29th, 30th, etc…), and the date of their last invoice? This can be a little complicated. For example, let’s say your invoice for a customer who pays monthly, and signed up for service on the 30th of January. Well you cannot simply take their last invoice 1/30/2007 and add one month using LastInvoice.AddMonths(1) (which will give you 2/28/2007), for instance, becuase when you do the same the following month it will be 3/28/2007 instead of 3/30/2007 like it should be.

Other issues arise when it comes to leap years. The following code takes care of all the problems mentioned above:

VB .NET

Public Enum LicensePaymentType As Integer
  Monthly = 1
  Annual = 2
End Enum   

Private Function determineNextInvoice(ByVal LicType As LicensePaymentType, _
          ByVal InvoiceDay As Integer, ByVal LastInvoice As DateTime) As DateTime
  If LicType = LicensePaymentType.Monthly Then
     If LastInvoice.Day < InvoiceDay Then
        Dim Temp As Integer = InvoiceDay - LastInvoice.Day
        Dim NewInvDate As DateTime = LastInvoice.AddMonths(1)
        Return NewInvDate.AddDays(Temp)
     Else
        Return LastInvoice.AddMonths(1)
     End If
  ElseIf LicType = LicensePaymentType.Annual Then
     Dim NumDaysNextYearsMonth As Integer = _
           DateTime.DaysInMonth(LastInvoice.AddYears(1).Year, LastInvoice.AddYears(1).Month)
     If LastInvoice.Day < InvoiceDay AndAlso NumDaysNextYearsMonth >= InvoiceDay Then
        Dim Temp As Integer = InvoiceDay - LastInvoice.Day
        Dim NewInvDate As DateTime = LastInvoice.AddYears(1)
        Return NewInvDate.AddDays(Temp)
     Else
     Return LastInvoice.AddYears(1)
     End If
  End If
End Function

  • 0 Comments
  • Filed under: .Net General, Tech
  • Want to get replace those “smart quotes” that MS Word uses in your .NET code? This will do it:

    VB .NET

    Dim newstr As String
    newstr = oldstr.Replace(Chr(147), "& quot;") ' take out extra space b/w & and quot;
    newstr = newstr.Replace(Chr(148), "& quot;") ' take out extra space b/w & and quot;

    C#

    string newstr;
    newstr = oldstr.Replace((char)147, "& quot;") // take out extra space b/w & and quot;
    newstr = newstr.Replace((char)148, "& quot;") // take out extra space b/w & and quot;

  • 1 Comment
  • Filed under: .Net General, Tech