In my app, I use lots of dateformat and dateparse. In this case, I use Datetime.DateFormat = "dd/MMMM/yyyy". If the phone language is English, it will display like 01/January/2015, but if the phone language is not, for example I change it to Vietnamese, it will display like 01/thang mot/2015 (thang mot = January) My question is, can I always display January regardless of the phone language. Thank you.
Would this work for you:
1. Create a map that matches the vietnamese months( key) to the US months (value).
2. Then read the map to convert your date. See example below:
B4X:
Dim sets As Map 'Globals
'In Activity_create
sets.Initialize
sets.Put("thang mot", "January")
sets.Put("tháng hai", "February")
sets.Put("hành khúc", "March")
'rest of map here
File.WriteMap(File.DirRootExternal, "MapMonths", sets)
Log(MyEnglishDate("14/hành khúc/2015")) 'Displays 14/March/2015
Sub MyEnglishDate(MyDate As String) As String
sets = File.ReadMap(File.DirRootExternal, "MapMonths")
Dim D() As String =Regex.Split("/",MyDate)
Return D(0) & "/" & sets.Get(D(1)) & "/" & D(2)
End Sub
You can just store the original date format, then change the format to what you want and then run your routines, once completed restore the default settings back again.
I can't try it myself now since I'm writing this from a tablet..
Out of curiosity, changing the default locale will impact other concurrent running services/apps?
I mean, we change default locale in our app then the user presses Home and starts another app.
Will the new app use the locale we set before or it starts with an original (untouched) copy of device locale?
And what about returning to our first app (the one which changed the locale)?
@udg@ducphu only want to get the month, that whole procedure happens extremely quickly so I don't think that there will be any issues. The code above is how I would do it and it would work perfectly fine too. Now obviously there are plenty of other ways to get the results, the above code it just my solution...
Hi Peter, I tried changing my phone language to vietnamese then run your code. It doesn't work. This is the log:
tháng hai
16/02/2015
I will try Erel's code later. But then, the same question like udg asked: will changing the default locale impact other concurrent running services/apps?
Hi all, I tried this solution and it runs well on my phone but on my friend's phone, I encounter this issue:
1. If I connect the phone to PC (via USB), it works well (at least so far...)
2. If I dont connect to PC, when I first start the apps, or when the apps is killed (sometimes), it shows vietnamese date format, which then causes dateparse error and app stops. After that I re-open the app, this time it correctly shows US date format.
Hi, for example
datetime.dateformat = "MMMM/yyyy"
label1.text = datetime.date(datetime.now)
dim a as string = label1.text.substring2(0,2)
datetime.dateformat = "dd/MMM/yyyy"
dim day as string = "01"
dim year as string = "2015"
dim b as long = datetime.dateparse(day & "/" & a & "/" & year)
if the language is US, a = Jan, Feb... If language is vietnamese, a = tha... which causes error.
I don't think that it is a good idea to let the user write the month name. It will be difficult. Either use a date picker (such as AnotherDatePicker) or use a simpler format.
About the locale setting, I don't see how it can be affected by USB debug mode. You need to make sure that you call it when immediately when your app starts.