Android Question Convert Date Format

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub ConvertDate(date As String) As String
    DateTime.DateFormat = "dd-MM-yyyy"
    Dim t As Long = DateTime.DateParse(date)
    DateTime.DateFormat = "MMM''yyyy" 'two quotes as the quote needs to be escaped
    Return DateTime.Date(t)
End Sub

Note that it changes the date format so make sure to explicitly set it before parsing dates in other places.
 
Upvote 0

emexes

Expert
Licensed User
Note that it changes the date format so make sure to explicitly set it before parsing dates in other places.

or, in the absence of DateParse and DateFormat functions that have a non-global format parameter, perhaps save and restore?

B4X:
Sub ConvertDate(date As String) As String
    Dim SaveDateFormat As String = DateTime.DateFormat
    DateTime.DateFormat = "dd-MM-yyyy"
    Dim t As Long = DateTime.DateParse(date)
    DateTime.DateFormat = "MMM''yyyy" 'two quotes as the quote needs to be escaped
    Dim Result As String = DateTime.Date(t)
    DateTime.DateFormat = SaveDateFormat
    Return Result
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I need to convert date(22-04-2023) into Apr'2023. Is it possible?

Not only is it possible, but it is possible in many ways, eg if you are confident of the date being in that exact 10-character format then you could add this Sub:

B4X:
Sub RishiFormat(D As String) As String
  
    Dim Mmm() As String = Array As String( _
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" _
    )
  
    Dim M As Int = D.SubString2(3, 5)    '2 digit month
    Dim Y As Int = D.SubString2(6, 10)    '4 digit year
  
    Return Mmm(M - 1) & "'" & Y

End Sub

and call it eg:

B4X:
Dim TestDate As String = "22-04-2023"
Log( TestDate & " becomes " & RishiFormat(TestDate) )
 
Upvote 0

emexes

Expert
Licensed User
I need to convert date(22-04-2023) into Apr'2023. Is it possible?

The nice thing about the hand-crafted array approach is that it makes oddball arbitrary formats easier eg you could freak your users out with:

B4X:
Sub NotRishiFormat(D As String) As String
 
    Dim Mmm() As String = Array As String( _
        "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Penultimate", "Last" _
    )
 
    Dim M As Int = D.SubString2(3, 5)    '2 digit month
    Dim Y As Int = D.SubString2(6, 10)    '4 digit year
 
    Return Mmm(M - 1) & " month of " & Y

End Sub
 
Last edited:
Upvote 0

RishiKumar2210

Active Member
Not only is it possible, but it is possible in many ways, eg if you are confident of the date being in that exact 10-character format then you could add this Sub:

B4X:
Sub RishiFormat(D As String) As String
 
    Dim Mmm() As String = Array As String( _
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" _
    )
 
    Dim M As Int = D.SubString2(3, 5)    '2 digit month
    Dim Y As Int = D.SubString2(6, 10)    '4 digit year
 
    Return Mmm(M - 1) & "'" & Y

End Sub

and call it eg:

B4X:
Dim TestDate As String = "22-04-2023"
Log( TestDate & " becomes " & RishiFormat(TestDate) )
Thanks for the answer and Function Name(rishiformat) @emexes. I will try this.
 
Upvote 0

RishiKumar2210

Active Member
Not only is it possible, but it is possible in many ways, eg if you are confident of the date being in that exact 10-character format then you could add this Sub:

B4X:
Sub RishiFormat(D As String) As String
 
    Dim Mmm() As String = Array As String( _
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" _
    )
 
    Dim M As Int = D.SubString2(3, 5)    '2 digit month
    Dim Y As Int = D.SubString2(6, 10)    '4 digit year
 
    Return Mmm(M - 1) & "'" & Y

End Sub

and call it eg:

B4X:
Dim TestDate As String = "22-04-2023"
Log( TestDate & " becomes " & RishiFormat(TestDate) )
Thanks. Mr. @emexes It Worked.
 
Upvote 0
Top