Add Months to Date - InfoPath Dev
in

InfoPath Dev

Add Months to Date

Last post 09-26-2012 01:59 PM by derickson. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 04-15-2009 10:33 AM

    Add Months to Date

    I'd like to have a date picker control for a contract date, a text field for contract months, and another field for contract expiration date where expiration date = contract date + contract months

    AddDays() simply won't cut it because I need the day to be the same every month.

     Could someone be nice and offer some code (vb preferably).

     Thanks

    Filed under: ,
  • 04-21-2009 11:01 AM In reply to

    You need to use the AddMonths method of the Date class. This should get you started:

    Once you enter the number of months for the contract, the code will produce the date by using the AddMonths method.

    Public Sub field2_Changed(ByVal sender As Object, ByVal e As XmlEventArgs)

    ' Write your code here to change the main data source.

    Dim DOM As XPathNavigator = MainDataSource.CreateNavigator()

    ' Create reference to date picker

    Dim datePickerNode As XPathNavigator = DOM.SelectSingleNode("/my:myFields/my:field1", NamespaceManager)

    ' remove nil attribute from node

    DeleteNil(datePickerNode)

    ' Create reference to contract months

    Dim contractMonthsNode As XPathNavigator = DOM.SelectSingleNode("/my:myFields/my:field2", NamespaceManager)

    ' Create reference to contract end date

    Dim contractEndDate As XPathNavigator = DOM.SelectSingleNode("/my:myFields/my:field3", NamespaceManager)

    ' remove nil attribute from node

    DeleteNil(contractEndDate)

    ' get the values

    Dim contractDate As Date = datePickerNode.ValueAsDateTime()

    Dim contractMonths As Integer = contractMonthsNode.ValueAsInt()

    ' do the math and return values

    contractEndDate.SetValue(contractDate.AddMonths(contractMonths).ToShortDateString)

    End Sub

    ' The following sub is required to work with dates!!

    Public Sub DeleteNil(ByVal node As XPathNavigator)

    If (node.MoveToAttribute( _

    "nil", "http://www.w3.org/2001/XMLSchema-instance")) Then

    node.DeleteSelf()

    End If

    End Sub

  • 04-21-2009 11:12 AM In reply to

    thanks for the reply!  this worked perfect.

  • 09-26-2012 01:59 PM In reply to

    Can i get this not in code?  I want to either create a rule to do this or a formula to do this.  I try the AddMonths but I get invalid syntax.

Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.