﻿Public Class Duodecimal
  Public Year As Integer
  Public Month As Integer
  Public DuoMonth As Double
  Public DuoDate As Date
  'NOTES DuoMonth is theYears*12 + (theMonth -1). months are 0 to 11

  Public Sub New()
    Me.Year = Date.Now.Year
    Me.Month = Date.Now.Month
    Me.DuoMonth = (Me.Year * 12) + Me.Month -1
    Me.DuoDate = New Date(Me.Year, Me.Month, 1)
  End Sub

  Public Sub New(ByVal inputDate As String)

    Me.Year = CInt(inputDate.Substring(0, 4))
    Me.Month = CInt(inputDate.Substring(4, 2))
    Me.DuoMonth = (Me.Year * 12) + Me.Month - 1
    Me.DuoDate = New Date(Me.Year, Me.Month, 1)
  End Sub

  Public Sub New(ByVal inputDate As Date)
    Me.Year = inputDate.Year
    Me.Month = inputDate.Month
    Me.DuoMonth = (Me.Year * 12) + Me.Month - 1
    Me.DuoDate = New Date(Me.Year, Me.Month, 1)
  End Sub

  Public Function getPeriod() As String
    Return Me.Year & Me.Month.ToString("00")
  End Function

  Public Shared Function Diff(ByVal strDate1 As String, ByVal strDate2 As String) As Double
    Try
      Dim Duo1 As New Duodecimal(strDate1)
      Dim Duo2 As New Duodecimal(strDate2)

      Return CDbl(Duo1.DuoMonth - Duo2.DuoMonth)

    Catch ex As Exception
      Return Double.NaN
    End Try
  End Function

  Public Shared Function Diff(ByVal Date1 As Date, ByVal Date2 As Date) As Double
    Try
      Dim Duo1 As New Duodecimal(Date1)
      Dim Duo2 As New Duodecimal(Date2)
      Return CDbl(Duo1.DuoMonth - Duo2.DuoMonth)

    Catch ex As Exception
      Return Double.NaN
    End Try
  End Function

  Public Function Diff(ByVal strDate1 As String) As Double
    Try
      Dim Duo1 As New Duodecimal(strDate1)
      Return CDbl(Me.DuoMonth - Duo1.DuoMonth)

    Catch ex As Exception
      Return Double.NaN
    End Try

  End Function

  Public Function Diff(ByVal Date1 As Date) As Double
    Try
      Dim Duo1 As New Duodecimal(Date1)
      Return CDbl(Me.DuoMonth - Duo1.DuoMonth)

    Catch ex As Exception
      Return Double.NaN
    End Try
  End Function

  Public Function Add(ByVal Months As Integer) As Double
    Try
      Me.DuoMonth += Months
      Me.Year = Me.DuoMonth \ 12
      Me.Month = ((Me.DuoMonth) Mod 12) + 1
      Me.DuoDate = New Date(Me.Year, Me.Month, 1)

      Return Me.Month
    Catch ex As Exception
      Return Double.NaN
    End Try

  End Function

  Public Shared Function Add(ByVal strDate1 As String, ByVal Months As Integer) As Date
    Try
      Dim Duo1 As New Duodecimal(strDate1)
      Duo1.DuoMonth += Months
      Duo1.Year = Duo1.DuoMonth \ 12
      Duo1.Month = ((Duo1.DuoMonth) Mod 12) + 1

      Return New Date(Duo1.Year, Duo1.Month, 1)
    Catch ex As Exception
      Return Nothing
    End Try
  End Function

  Public Shared Function Add(ByVal Date1 As Date, ByVal Months As Integer) As Date
    Try
      Dim Duo1 As New Duodecimal(Date1)
      Duo1.DuoMonth += Months
      Duo1.Year = Duo1.DuoMonth \ 12
      Duo1.Month = ((Duo1.DuoMonth) Mod 12) + 1

      Return New Date(Duo1.Year, Duo1.Month, 1)
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
End Class
