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.
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
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) )
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
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) )
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) )