Android Code Snippet Return the current month as a string

SubName: Returns the current month as a full string
Description: The following returns the current month as a full string, thus there's no need to worry about putting the months in a string array to loop through it.

The following code returns the month as January, February, March, April, May, June, July...
B4X:
'First store the original date format.
    Dim Ref As Reflector
        Ref.Target = Ref.RunStaticMethod("android.text.format.DateFormat", "getDateFormat", Array As Object(Ref.GetContext), Array As String("android.content.Context")) 'Returns default system date pattern
'Then set the date format and store/print the month
    DateTime.DateFormat = "MMMM"
    Log(DateTime.Date(DateTime.Now)) 'February
'Now restore the original date format.
    DateTime.DateFormat = Ref.RunMethod("toPattern")
    Log(DateTime.Date(DateTime.Now)) '15/02/2015 in my case
Tags: date format, return month
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Yes you're 100% correct, but if like me you don't want to load the DateUtils library(yes reflector is also a library I no I no I no) just to get one simple result like the month in text, then this will do just fine, especially if you're already using the Reflector library for something else. So yes you don't need to do it this way, but just like other things in B4A, there are plenty of ways go get the results you're look for @Erel :)

If you're not using the Reflector library already, then that's great, but I usually have the reflector library already loaded so I will just utilise that as much as possible instead of just loading more and more libraries no for reasons. But you are correct as I stated earlier.

Now I'm going to drink a nice cold beer as I've just got back from a quick bike ride :cool:
 
Last edited:
Top