Is there an easy way (macOS AND Windows) to pass an integer 1-12 and get back
the month name in the current locale?
short = getShortMonthName(month)
name = getLongMonthName(month)
It has to be independant of what ever date/time formats the user has set in preferences
[code]Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function alloc Lib “Foundation” Selector “alloc” (NSClass As Ptr) As Ptr
Declare Function init Lib “Foundation” Selector “init” (NSObject As Ptr) As Ptr
Declare Function objectAtIndex Lib “Foundation” Selector “objectAtIndex:” (NSArray As Ptr, idx As UInt32) As CFStringRef
Declare Function monthSymbols Lib “Foundation” Selector “monthSymbols” (NSDateFormatter As Ptr) As Ptr
Declare Function shortMonthSymbols Lib “Foundation” Selector “shortMonthSymbols” (NSDateFormatter As Ptr) As Ptr
Dim dateFormatter As Ptr = init(alloc(NSClassFromString(“NSDateFormatter”)))
Dim monthSymbol As String = objectAtIndex(monthSymbols(dateFormatter), 0)
Dim shortMonthSymbol As String = objectAtIndex(shortMonthSymbols(dateFormatter), 0)[/code]
You can use setLocale: on NSDateFormatter to use a specific locale.
this return January… need to change the 0 to month-1 in last two lines
Anyone have the equivalent for Windows???
I have a series of methods that validate date/time string and returns sqldate and sqldatetime formats for valid input values
Once I get / find a locale method for windows, I will post the code for all to see/use
It should be able to deal with dates entered in the users language based on locale (or English regardless of locale setting)
- IsValidDate(inDate as String, byref OutDate as String)
- IsValidTime(inTime as String, byref OutTime as String)
- IsValidDateTime(inDateTime as String, byref OutDateTime as String)
For all 3 of these functions, a blank input string is considered VALID input, and will return TRUE
IsValidDate ignores any time component and returns SQLDate formatted value and true if a valid date is entered, otherwise returns the input value and false
IsValidTime ignores any date component and returns a string compatible with SQL date/time formats [hh:mm:ss] and true if a valid date is entered, otherwise returns the input value and false
IsValidDateTime will default the date to TODAY if no date component is found, and the time to 00:00:00 if no time component is found, and will return a SQLDateTime formatted string if valid.
That returns the whole date, I want/need ONLY the Month name, not to mention I do not have a “date”, just a month index
Xojo.Core.Date is the only way I know of to impose your own locale to a date in Xojo. Otherwise, what is wrong with a 12 slots array of months names ?
12 slots x who knows how many languages?
If this were only for one language it would not be an issue at all… but I wish it to work for what ever language the user has defined, and do not wish the over head of 24 constants (long and short) for each one.
What Eli posted above works great for macOS…
I looked at what Mike Cotrone did for his calendar control… and he code with hardcode values for only 10 languages, but that is almost 600 lines of code just to define the “constants”… where IF they exist, Windows declares would be 1/10th that, and would not be restricted to only 10 predefinded languages.
Now if there were an easy way to parse the long or short name from a string returned by the date object, great, but you cannot “assume” what formats the user specified, and you (the developer) cannot specify a “known” one to use
Once again, Xojo.Core.Date is cross platform, and lets you set your own locale in all languages supported by Xojo. I tend to believe reinventing the wheel is somewhat futile.
Anyway, good luck.
Michel… you are missing my point… I do not have a DATE to convert… I do not (cannot) “parse” a date returned by the Date Object…
There is no “wheel” here to reinvent… Xojo.Core.Date works on DATES which have Months and Days and Years, and can be expressed in what ever format the user set in Sys Pref.
I do not want or need the Day or Year, and there is not consistent, accurate, language agnostic way to parse that
Heck the user could have set Long Date format to MM/DD/YY in which there would be no Month Name at all
As I said, Good Luck. Rigid minds need a lot of it.
Michel… if you are so h*ll bent on knowing that that will “work”, PLEASE, PLEASE educate me… show me the error of my ways… but do not insult me.
What of these statements are false?
- Xojo Date objects (new or old framework) return either SQLDATE, SQLDATETIME formats OR a date/time format set by the user in SysPref
- Xojo Date objects cannot have an arbitary output format assigned in Xojo code (ie. you cannot create a MONTH only output format)
- The format used is either the defaults for the OS, -or- anything the user may have decided to set them to
As far as I know, my rigid mind (and you know that I am the least rigid minded person you know), believes these to be 100% true statements, and am willing to accept any examples that prove otherwise.
- Xojo.Core.Date.ToText I linked to lets you set your own locale (fr-FR in the example below).
- Parsing for instance “1 août 2015” should not be that difficult
- Xojo.Core.Date.ToText lets you choose the format (Xojo.Core.Date.FormatStyles.Long)
[code]Dim mylocale As New Xojo.Core.Locale(“fr-FR”)
dim d as xojo.core.date = xojo.core.date.FromText(“2015-08-01 11:00”)
Dim t As Text = d.ToText(myLocale, Xojo.Core.Date.FormatStyles.Long, Xojo.Core.Date.FormatStyles.None)
dim components() as Text = t.Split(" ")
Seems to me a method using Xojo.Core.Date to generate month names from any locale is utterly possible without 600 lines.
Sorry, but rejecting outright a solution without exploring it does not seems to me to indicate an open mind.
dim d as xojo.core.date = xojo.core.date.FromText("2015-08-01 11:00")
Dim t As Text = d.ToText(xojo.core.Locale.Current, Xojo.Core.Date.FormatStyles.Long, Xojo.Core.Date.FormatStyles.None)
no way to extract “August” or " août" or any other name…
for testing… I set it to 01 5 2017 in Sys Pref… something that any user may or may not do,
by the same token, they may replace LongName with Shortname in the LONG format,
and Shortname with LongName in the SHORT format…
making anything that relys on Sys Pref not reliable for this purpose.
So, I still am not seeing this as a solution, since the user can so easily break it
If it does not show the month name, that is a bug, as this is what Xojo.Core.Date.FormatStyles.Long should show:
The long date or time format:
December 1, 2014
12:23:53 PM EST
Michel… what part of the USER CAN CHANGE IT do you not get?
The user CAN change them… to prove that I DID change it, and got results that while correct, where not useable for the intended purpose… So no, it is not a bug, it works just like it should, and as I would have expected. Xojo-core-date uses the format specified in SysPref, as I have been trying to get across since this topic began.
The formats documented in the LR as the OS DEFAULT settings, assuming that the user DID NOT change them
You don’t needs “Dates”
Dates need “formatters” so YOU could programmatically override user choices about how dates should be formatted (ie, so you could then parse out the month name from a date)
Or you can try reading all these bits from a Calendar (which actually defines all these things - day names month names etc)
Norman… this is what I have been trying to convey…
I don’t HAVE a date… .I have a month index (1 to 12) and I wish the NAME of that month localized to the user…
in macOS its easy-peasy (see above… way above )
And Apple documentation won’t help me with a WINDOWS solution… I have an Apple solution already, which if you will notice does not deal with “Dates” but it deals with “formatters” just as you indicated.
Will confess I did not read the whole thread
On Windows there are definitely Calendar related functions (https://msdn.microsoft.com/en-us/library/windows/desktop/dd318075(v=vs.85).aspx)
And using the right parameter for the Calendar Info Type you can get the names of weekdays and months
And FWIW at on time Aarons WFS had a VB function for exactly this
You might look there first
No need to become adversarial.
May I suggest you buy Eugene Dakin book about declares and build your own low level Windows method.