Date formatting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mayur2007
    New Member
    • Aug 2007
    • 67

    Date formatting

    Hello Everyone,

    I have one problem related to date function in vb
    i want to print the date in the following format
    15th, October-2007
    i have already try formatter of the date funtion
    Can anyone help me out?? please be fast.

    Mayur
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    written in vb. vb.net should be simular...

    [code=vb]
    Dim a As String
    a = Mid(System.Date Time.Now.ToStri ng, 1, 10) 'cuts out date value (dd/mm/yyyy)

    Dim b As String
    b = Mid(a, 4, 2) 'stores month value (mm)

    Dim c As String

    If CInt(b) = "01" Then
    c = Mid(a, 1, 2) + "th," + " January " + Mid(a, 7, 4)
    ElseIf CInt(b) = "02" Then
    c = Mid(a, 1, 2) + "th," + " February " + Mid(a, 7, 4)
    ElseIf CInt(b) = "03" Then
    c = Mid(a, 1, 2) + "th," + " March " + Mid(a, 7, 4)
    ElseIf CInt(b) = "04" Then
    c = Mid(a, 1, 2) + "th," + " April " + Mid(a, 7, 4)
    ElseIf CInt(b) = "05" Then
    c = Mid(a, 1, 2) + "th," + " May " + Mid(a, 7, 4)
    ElseIf CInt(b) = "06" Then
    c = Mid(a, 1, 2) + "th," + " June " + Mid(a, 7, 4)
    ElseIf CInt(b) = "07" Then
    c = Mid(a, 1, 2) + "th," + " July " + Mid(a, 7, 4)
    ElseIf CInt(b) = "08" Then
    c = Mid(a, 1, 2) + "th," + " August " + Mid(a, 7, 4)
    ElseIf CInt(b) = "09" Then
    c = Mid(a, 1, 2) + "th," + " September " + Mid(a, 7, 4)
    ElseIf CInt(b) = "10" Then
    c = Mid(a, 1, 2) + "th," + " October " + Mid(a, 7, 4)
    ElseIf CInt(b) = "11" Then
    c = Mid(a, 1, 2) + "th," + " Novermber " + Mid(a, 7, 4)
    ElseIf CInt(b) = "12" Then
    c = Mid(a, 1, 2) + "th," + " December " + Mid(a, 7, 4)
    End If
    [/code]

    value stored in c

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      Written in vb but vb.net should be very similar...

      [code=vbnet] Dim a As String
      a = Mid(System.Date Time.Now.ToStri ng, 1, 10) 'cuts out date value (dd/mm/yyyy)
      Dim b As String
      b = Mid(a, 4, 2) 'stores month value (mm)
      Dim c As String
      If CInt(b) = "01" Then
      c = Mid(a, 1, 2) + " January " + Mid(a, 7, 4)
      ElseIf CInt(b) = "02" Then
      c = Mid(a, 1, 2) + " February " + Mid(a, 7, 4)
      ElseIf CInt(b) = "03" Then
      c = Mid(a, 1, 2) + " March " + Mid(a, 7, 4)
      ElseIf CInt(b) = "04" Then
      c = Mid(a, 1, 2) + " April " + Mid(a, 7, 4)
      ElseIf CInt(b) = "05" Then
      c = Mid(a, 1, 2) + " May " + Mid(a, 7, 4)
      ElseIf CInt(b) = "06" Then
      c = Mid(a, 1, 2) + " June " + Mid(a, 7, 4)
      ElseIf CInt(b) = "07" Then
      c = Mid(a, 1, 2) + " July " + Mid(a, 7, 4)
      ElseIf CInt(b) = "08" Then
      c = Mid(a, 1, 2) + " August " + Mid(a, 7, 4)
      ElseIf CInt(b) = "09" Then
      c = Mid(a, 1, 2) + " September " + Mid(a, 7, 4)
      ElseIf CInt(b) = "10" Then
      c = Mid(a, 1, 2) + " October " + Mid(a, 7, 4)
      ElseIf CInt(b) = "11" Then
      c = Mid(a, 1, 2) + " Novermber " + Mid(a, 7, 4)
      ElseIf CInt(b) = "12" Then
      c = Mid(a, 1, 2) + " December " + Mid(a, 7, 4)
      End If[/code]

      value stored in c
      Last edited by Killer42; Oct 9 '07, 10:36 PM.

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        select the following from oracle for the exact format

        [CODE=oracle]select to_char(sysdate ,'ddth, month yyyy') from dual[/CODE]

        Comment

        • jamesd0142
          Contributor
          • Sep 2007
          • 471

          #5
          i played about with this for a while, teaching myself how to use functions in vb.

          i come up with this and it works ok...

          ---------------------------------------------------------------------------------------------
          [code=vb]
          Dim b As String
          b = System.DateTime .Now.Day
          Dim c As String
          c = System.DateTime .Now.Month
          Dim d As String
          d = System.DateTime .Now.Year
          MsgBox(b + " " + getMonth(c) + " " + d)
          [/code]
          --------------------------------------------------------------------------------------------

          AND THEN THE FUNCTION

          --------------------------------------------------------------------------------------------
          [code=vb] Function getMonth(ByVal Val)
          Dim c As String
          If CInt(Val) = "01" Then
          c = "January"
          ElseIf CInt(Val) = "02" Then
          c = "February"
          ElseIf CInt(Val) = "03" Then
          c = "March"
          ElseIf CInt(Val) = "04" Then
          c = "April"
          ElseIf CInt(Val) = "05" Then
          c = "May"
          ElseIf CInt(Val) = "06" Then
          c = "June"
          ElseIf CInt(Val) = "07" Then
          c = "July"
          ElseIf CInt(Val) = "08" Then
          c = "August"
          ElseIf CInt(Val) = "09" Then
          c = "September"
          ElseIf CInt(Val) = "10" Then
          c = "October"
          ElseIf CInt(Val) = "11" Then
          c = "Novermber"
          ElseIf CInt(Val) = "12" Then
          c = "December"
          End If

          Return c
          End Function
          [/code]
          --------------------------------------------------------------------------------------------

          Comment

          • Tig201
            New Member
            • Mar 2007
            • 103

            #6
            Originally posted by Mayur2007
            Hello Everyone,

            I have one problem related to date function in vb
            i want to print the date in the following format
            15th, October-2007
            i have already try formatter of the date funtion
            Can anyone help me out?? please be fast.

            Mayur
            You could try modifying this...
            [CODE=vb]
            Dim Str As String, Str1 As String

            Select Case Right$(Format(N ow(), "d"), 1)
            Case Is = "1"
            Str1 = "st"
            Case Is = "2"
            Str1 = "cnd"
            Case Is = "3"
            Str1 = "rd"
            Case Else
            Str1 = "th"
            End Select
            Str = Format(Now(), "d") & Str1 & ", " & Format(Now(), "mmmm-yyyy")

            [/CODE]

            It is from VB6

            Comment

            • jamesd0142
              Contributor
              • Sep 2007
              • 471

              #7
              I modified it like this...

              [code=vb]
              Dim b As String
              b = "2" 'System.DateTim e.Now.Day
              Dim c As String
              c = System.DateTime .Now.Month
              Dim d As String
              d = System.DateTime .Now.Year
              MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)
              [/code]

              MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)

              and add this function

              [code=vb]
              Function ending(ByVal Val)
              Dim a As String
              If CInt(Val) = "01" Then
              a = "st"
              ElseIf CInt(Val) = "02" Then
              a = "nd"
              ElseIf CInt(Val) = "03" Then
              a = "rd"
              Else
              a = "th"
              End If
              Return a
              End Function
              [/code]

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                If these numbers relate to the day, then don't forget you have 11th and 21st. Also 12th and 22nd. Also 13th and 23rd. English is such fun, isn't it? :)

                I'd recommend the use of a Select Case statement, as it allows very simple handling of multiple cases like this. for example Case 1, 21 to return "st".

                Also, in general it would be better practice to execute your function once at the start, placing the result in a variable, then work with that variable while doing the various ElseIf's, rather than performing multiple function calls. Also, it's probably a really bad idea to call a variable Val, given that Val() is also the name of a commonly-used function.

                My recommendation for a rewrite of this function would be something like...

                [CODE=vb]Public Function Ending(ByVal DayNum) As String
                ' Dim a As String
                Select Case CInt(DayNum )
                Case 1, 21, 31
                Ending = "st"
                Case 2, 22
                Ending = "nd"
                Case 3, 23
                Ending = "rd"
                Case Else
                Ending = "th"
                End Select
                End Function[/CODE]On the other hand, it would probably make more sense to use an array rather than a function. Why bother doing the work again, every time you access a date?
                Last edited by Killer42; Oct 10 '07, 07:09 AM.

                Comment

                • Mayur2007
                  New Member
                  • Aug 2007
                  • 67

                  #9
                  Hi,

                  Thank you "jamesd0142 " and "Killer42".
                  I have solved my problem with your help.
                  As you said killer42 I have made the function so its works fine for me.
                  See jamesd0142 the whole code will look like this for the VB.
                  Thanks again.

                  [code=vb]Private Sub Form_Load()
                  Dim b As String
                  b = Format(Date, "dd") & Ending(Format(D ate, "dd"))
                  Dim c As String
                  c = Format(Date, "mm")
                  Dim d As String
                  d = Format(Date, "yyyy")
                  MsgBox b & Format(Date, " - mmmm,yyyy")
                  End Sub

                  Public Function Ending(ByVal DayNum) As String
                  Select Case CInt(DayNum)
                  Case 1, 21, 31
                  Ending = "st"
                  Case 2, 22
                  Ending = "nd"
                  Case 3, 23
                  Ending = "rd"
                  Case Else
                  Ending = "th"
                  End Select
                  End Function[/code]
                  Last edited by Killer42; Oct 10 '07, 07:11 AM. Reason: Fixed /code tag.

                  Comment

                  • jamesd0142
                    Contributor
                    • Sep 2007
                    • 471

                    #10
                    Great!

                    This is the problem with me... my code neva looks this neat :P

                    I tend to use 53 lines where 12 will do :)

                    Comment

                    Working...