Hey all,
How do you convert an integer to a month string? Without a list if possible.
ex.
Entered value > Resulting String
1 > January
6 > June.
12 > December
TIA
Hey all,
How do you convert an integer to a month string? Without a list if possible.
ex.
Entered value > Resulting String
1 > January
6 > June.
12 > December
TIA
This seems to work:
set monthNumber to 4
set monthText to (month of ("0001-" & (text -2 thru -1 of ("0" & monthNumber)) & "-01" as «class isot» as date)) as Unicode text
set theNumber to 4
set theDate to (current date)
set the month of theDate to theNumber
set theMonth to month of theDate as string
or if you like one-liners
set theNumber to 3
set theMonth to month of date ((theNumber & ",1") as string) as string
My word, you have been playing with ISO 8601, Bruce.
In that case, you might like this variation:
set m to 4
(m * 100 + 10000001 as string as «class isot» as date)'s month as string
Why do you want to exclude the simplest and fastest way of doing it?
set m to 4
item m of {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
Since I already know how to I thought I could learn a little more advance applescript with the help of everyone.
thanks again all for all the help.
Don’t forget this one:
set m to 12
((«data isot3130303031323031» as date) + m * 2764800)'s month as string
For a Laugh And yes I know it is Uk date format
set m to 4
set dateTemp to month of date ("1/" & m & "/2007") as string
Hey, no copying mark! That’s the same as my one-liner above, except I left the year out because it’s not necessary and I used a comma instead of a “/” (smiling).
Hey, no copying mark! That’s the same as my one-liner above, except I left the year out because it’s not necessary and I used a comma instead of a “/” (smiling).
I did not copy you script, I did not even notice it. until you pointed it out. (my bad)
If I had I would have not even spent the 5 minutes working it out, since your approach is much better for writing into a script.
Hey mark, I was just playing. That’s why I put (smiling) in my comment. I wasn’t being serious.
Anyway, since we’re learning here. If you run this code…
set theDate to "1"
date (theDate)
→ date “Sunday, April 1, 2007 12:00:00 AM”
→ i.e. it uses the current month and year and sets the day to “1”
set theDate to “1,2”
→ changes the month to “1” and the day to “2” and uses the current year
You can go all the way out to (1,2,3,4,5,6)
→ month,day,year,hours,mins,secs
And instead of a comma separating the numbers, you can use a " " or “/” or “!” or “@”… I guess just about any symbol
But if you separate the numbers with a “:” it changes the order
from: month,day,year,hours,mins,secs
to: hours:mins:secs:month:day:year
I know, I know. That’s more than you wanted to know!
This behaviour is due to the way in which AppleScript creates date objects. When presented with partial data, it attempts to fill in the blanks.
A single value is generally used to specify the day, while the month and year values are assumed to be those for the current date. However, the date object from multiple input values can vary “ since, just as a date’s output format is affected by the current language/format settings, so is its input. (The raw values of date objects in compiled scripts are not affected “ only those for dates created at compile time or runtime.)
For example:
set theDate to "1,2" -- or use some other separator apart from":"
date (theDate)
--> [United Kingdom] date "Thursday, February 1, 2007 00:00:00"
--> [Portugal] date "2 de Janeiro de 2007 0:00:00"
Or:
set theDate to "1,2,3,4,5,6"
date (theDate)
--> [United Kingdom] date "Saturday, February 1, 2003 04:05:06"
--> [Portugal] date "3 de Fevereiro de 2001 4:05:06"
So if we want our code to be more portable, the input date value needs to be formatted less ambiguously.
Instead of the above examples, we might use something like:
set theDate to current date ” or any other date
tell theDate to set {its month, day, time} to {2, 1, 0}
theDate
--> [United Kingdom] date "Thursday, February 1, 2007 00:00:00"
--> [Portugal] date "1 de Fevereiro de 2007 0:00:00"
Or:
set theDate to current date ” or any other date
tell theDate to set {year, its month, day, its hours, its minutes, its seconds} to {2003, 2, 1, 4, 5, 6}
theDate
--> [United Kingdom] date "Saturday, February 1, 2003 04:05:06"
--> [Portugal] date "1 de Fevereiro de 2003 4:05:06"
Alternatively, as Nigel demonstrated in the recent discussion entitled “current date - 24 hr clock”:
set theDate to "2007-02-01" as «class isot» as date
--> [United Kingdom] date "Thursday, February 1, 2007 00:00:00"
--> [Portugal] date "1 de Fevereiro de 2007 0:00:00"
Or:
set theDate to "2003-02-01T04:05:06" as «class isot» as date
--> [United Kingdom] date "Saturday, February 1, 2003 04:05:06"
--> [Portugal] date "1 de Fevereiro de 2003 4:05:06"
Thanks guy’s
So for the OP or anyone to get the right order of day and month when trying to find the string for the integer of a month, which may vary.
Would this work
set theMonth to {"2"}
set theday to {"9"}
set thedate to (current date)
tell thedate to set {its month} to theMonth
tell thedate to set {its day} to theday
thedate
That works, Mark “ although it’s worth bearing in mind that AppleScript will adjust the value of a date object mathematically.
So to perform such an operation, those single-item lists of text values will first be coerced, behind the scenes, to integers: {“2”} ”> “2” ”> 2. It’s therefore probably wiser to use integers in the first place.
While I used a tell statement and list to attribute multiple values to various date object properties, those techniques aren’t perhaps as relevant when performing individual attributions. We could, for example, simply use something like:
set theMonth to 2
set theday to 9
set theDate to current date
set theDate's month to theMonth
set theDate's day to theday
theDate
When adjusting a date in this way, remember that the value of unchanged properties will be those of the original date object. So the year and time of the above date will be those set when the script is run.
However, while the output format might differ according to local settings, the raw value of the date object will now be the same:
→ [United Kingdom] date “Friday, February 9, 2007 17:45:30” (depending on when script is run)
→ [Portugal] date “9 de Fevereiro de 2007 17:45:30” (ditto)
This means that subsequently extracting the month will produce the same result in either case:
set theMonth to 2
set theDate to current date
set theDate's month to theMonth
theDate's month as string
--> [United Kingdom] "February"
--> [Portugal] "February"
Because we’re merely coercing one of the month constants (January, February, March, April, May, June, July, August, September, October, November, December) to a string, the result is not dependent on localisation.
Getting the localised month name, however, might be a bit more problematic. Here’s a minor adaptation of the date string parsing routine, from the recent discussion about “current date - current month text value to numerical value”:
on local_month(n)
set tid to text item delimiters
set text item delimiters to "1"
tell «data isot3131313130313031» as date to considering case
set l to (it + 23587200)'s date string's text items
tell date string's text items
repeat with t from 1 to count
tell item t to if it is not in l then exit repeat
end repeat
set l to l's item t's words
tell item t's words
set c to count
repeat with b from 1 to c
if item b is not in l then exit repeat
end repeat
repeat with e from -1 to -c by -1
if item e is not in l then exit repeat
end repeat
end tell
end tell
set its month to n
set m to text from word b to word e of date string's text item t
end considering
set text item delimiters to tid
m
end local_month
local_month(9)
--> "September" (English, Deutsch)
--> "september" (Nederlands, Sverige, Norge, Dansk)
--> "septembre" (Français)
--> "septiembre" (Español)
--> "settembre" (Italiano)
--> "Setembro" (Portuguêse)
--> "syyskuu" (Suomi)
--> "Meán Fómhair" (Gaeilge)
Getting the localised month name, however, might be a bit more problematic. Here’s a minor adaptation of the date string parsing routine, from the recent discussion about “current date - current month text value to numerical value”:
Wow, Kai. Two Sunday the firsts in the year that’s all ones! How long did it take you to think up that one? :lol:
Here’s a simpler variation that seems (as far as I can tell) to be just as reliable. (But, like your version, it assumes that the local expressions for “January” and “October” don’t begin or end with the same word.)
on local_month(n)
tell «data isot3131313130313031» as date
set ds1w to (it as Unicode text)'s words -- Words of local date/time string for Sunday 1st January 1111.
set ds10w to ((it + 23587200) as Unicode text)'s words -- Words of local date/time string for Sunday 1st October 1111.
set dsm to (it + 2419200 * n) as Unicode text -- Local date/time string for some Sunday in target month 1111.
end tell
considering case
set b to 1
repeat while (item b of ds1w is item b of ds10w)
set b to b + 1
end repeat
set e to -1
repeat while (item e of ds1w is item e of ds10w)
set e to e - 1
end repeat
end considering
return text from word b to word e of dsm
end local_month
set l to {}
repeat with i from 1 to 12
set end of l to local_month(i)
end repeat
l
Edit: Corrected previous waffle about “Mondays” to “Sundays”. Found optimisation for the maths when setting dsm.
Wow, Kai. Two Sunday the firsts in the year that’s all ones! How long did it take you to think up that one? :lol:
:lol: The basic idea took next to no time at all, Nigel. (Must have been one of my better days.) Took slightly longer to come up with an appropriate date, though!
Now that you’ve removed the use of text item delimiters, I don’t suppose it matters that much which year is used as a base “ although this one seems as good as any.
Here’s a simpler variation that seems (as far as I can tell) to be just as reliable…
As always, a very neat optimisation, Mr. G!
I think that I may also have found a cheat for the calculation that finds a Sunday in the target month…
on local_month(n)
tell «data isot3131313130313031» as date
set l to words of (it as Unicode text)
tell words of ((it + 23587200) as Unicode text) to considering case
set b to 1
repeat while (item b is l's item b)
set b to b + 1
end repeat
set e to -1
repeat while (item e is l's item e)
set e to e - 1
end repeat
end considering
text from word b to word e of ((it + n * 2419200) as Unicode text)
end tell
end local_month
set l to {}
repeat with i from 1 to 12
set l's end to local_month(i)
end repeat
l
Ah - just as I’m about to post, I see you’ve already incorporated it in a ninja edit. :lol:
Now it’s my turn for a ninja edit: Removed waffle about Unicode versus plain text.
I see you’ve already incorporated it in a ninja edit. :lol:
“Ninja edit,” huh? Hmmm… I like it!