Formatting year from 4 digit to 2 digit - InfoPath Dev
in

InfoPath Dev

Formatting year from 4 digit to 2 digit

Last post 02-07-2008 08:16 AM by barmatt80. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 02-06-2008 10:08 AM

    Formatting year from 4 digit to 2 digit

    I am creating a custom name for a form.  An example of the name is: PR-00S-0208-4593.  The PR stand for purchase request, the 00S refers to type of purchase request, 0208 refers to month and year request was made, and the 4593 is a random generated number.  I got all the parts figured out and successfully got it to generate the number to PR-00S-022008-4593. 

     I am needing to convert the 4 digit year to a 2  digit year.  Any suggestions?  I coded everything within visual studio 2005 using vb and it worked flawless.  I started copying over to infopath and I get an error when formatting the year to two digits.  Below is my code:

    Public strMonth As String = System.DateTime.Now.Month

    Public strMonthNumber As String

    Public datYear As Date = System.DateTime.Now

    Public strFormatted As String = Format(datYear, datYear, "yy")

     

    Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer

    ' Returns a random number,

    ' between the optional Low and High parameters

    Return objRandom.Next(Low, High + 1)

    End Function

     Then on the load event handler, i have this code:

    Dim intRandom As Integer

    intRandom = GetRandomNumber(1001, 9999)

    thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:PRNUMBER").text = "PR-00S-" & strMonthNumber & datYear & "-" & intRandom.ToString

     

    The error I get is: Value of type 'Date' cannot be converted to 'System.Type' from when I try to format the year to 2 digits.

     

    Any suggestions?  Thanks.

  • 02-06-2008 12:54 PM In reply to

    Re: Formatting year from 4 digit to 2 digit

    I would just use a substring function and take the last two characters of the date field.

    02/06/2008  substring(date, 9, 2)

    Or if the date is 2008-02-06 substring(date, 3, 2)

    the syntax is for InfoPath rules but would be the same for code.  Take the characters from the string starting at position and for x characters.  SInce the date is always consistent and the 2 digit year will always be in the same place it should be pretty simple.

  • 02-07-2008 08:16 AM In reply to

    Re: Formatting year from 4 digit to 2 digit

    thanks, I got it to work.

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