April Fool's dates

I typed these lines into Script editor:

date"just a test"
date “only a test”
date “april fool”
date “a test”

and compiled the script, which became:

date “Thursday, April 1, 2004 12:00:00 AM”
date “Thursday, April 1, 2004 12:00:00 AM”
date “Thursday, April 1, 2004 12:00:00 AM”
date “Thursday, April 1, 2004 12:00:00 AM”

This all started when I included in one of my scripts a dialog box where the user is supposed to type in a date. I want to check the text returned to make sure it is really a date, so I try to coerce it. Which works until you type in “just a test” or one of these other strings.

Is this a bug throughout the Panther world? Or something wacky on my machine?

That’s wacky. I’m not sure it’s a “bug”. It seems like a joke that the ‘wild and crazy’ guys over at apple wrote into the backend… assuming that many people, but only developers, would see and perhaps appreciate it’s humor. It does appear on my machine (10.3.5), and I’d be curious to know how far this goes back. Many variations/combinations of the words in the string’s you’ve mentioned will produce the same results. Try “test a fool”, “april test”, etc.

Thanks for the insight… :slight_smile:
j

Well, the humor is lost on me. I’m just glad I stumbled onto this in the privacy of my own cubicle. I could imagine demo-ing a script for a roomful of Mac haters and naively typing “just a test” into a dialog box as a way of demonstrating how AppleScript can help users avoid mistakes.

That’s really strange, but if you play with it a bit further you’ll begin to see a pattern. Try

date "f2" --compiles as...
-->date "Monday, February 2, 2004 12:00:00 AM"

or

date "j3" --compiles as...
-->date "Saturday, January 3, 2004 12:00:00 AM"

Then look at at

date "j 22 -10"--compiles as...
--> date "Friday, January 22, 2010 12:00:00 AM"

but

date "jul4" ---compiles as...
-->date "Sunday, July 4, 2004 12:00:00 AM"

Appearently, you can use the abbreviation of a month in the “date” command. (j, f, m, ap, may, ju, jul, au, s, o, n, d) and AppleScript will over look any string that is not a part of a month or give you a date format error – in your case it is picking up the letter “a” for April, and “just & test” are ignored. example

date "I love my ma" --compiles as...
-->date "Monday, March 1, 2004 12:00:00 AM" --"I love my" are not part of a month string so the are ignored

Whereas 

date "No Way!" --compiles as...
-->date "Monday, November 1, 2004 12:00:00 AM"

Here’s another twist, to get the current date at midnight try

date"0" --compiles as...
-->date "Friday, September 24, 2004 12:00:00 AM" --depending on when you run the script

All in all, it’s faily bazaar, I’m glad you pointd it out. It may even be useful – Thanks Robert, and to ‘heck’ with them ~Mac Haters~

Actually, this is not as humorous as it appears. It actually is just a coincidence. Take this example:

In this script, if the name of a month (or the abbreviation for the month) is found in the string, then that string is coerced to the first day of the month found. So, any string with the word “april” in it will be coerced to April 1 which happens to be April Fools’ Day. The “fool” in the string is just a coincidence. Likewise, since “a” is the abbreviation for April, any string with “a” in it as a separate word (such as “a test” or “this is a string”) will be coerced to April 1 when coerced to a date, the word “test” is inconsequential.

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

It seems be recent attempt to make AS’s date parsing even more intelligent. I get an “Invalid date and time” error in 10.2.8 (AS 1.9.1), 9.2.2, and 8.6. (I haven’t tried reconfiguring to US date format to see if that makes any difference.)

On the older systems, a single letter is not enough to identify a month. 'date “f2” compiles as ‘date “Thursday, 2 September 2004 00:00:00”’. The compiler ignores the “f” — which it doesn’t understand — and just works with the “2”. The default in this case (with UK configurations at least) is to compile ‘date “2”’ as the second of the current month at midnight. When the date string contains either not enough or contradictory information, I get an error.

Here’s a nifty use for it…

display dialog "Enter your birthdate (m/d/year/time)" default answer "2/4/1953/1:33"
set x to text returned of result
set y to get the weekday of date x
set z to get the time string of date x
if z contains "AM" then
	set a to "Morning"
else if z contains "PM" then
	set a to "Afternoon"
end if
display dialog "You were born on a " & y & space & a

This is using the US date format, sorry if it breaks for you NG

That’s OK, Greg. I already knew I was born on a Wednesday evening. :wink:

sorry if this is sort of a newbie question but how would you get it to give you the current date and time in a dialog box, without them having to enter anything? like so it automatically takes it from the systems current date and time.

There are a few ways to get the date in the dialog…

set x to (current date) as string
display dialog "" default answer x
--> "Saturday, September 25, 2004 11:05:54 PM"

or

set x to date "0" as string
display dialog "" default answer x
-->"Saturday, September 25, 2004 11:05:54 PM"

This is most likely what your looking for, but it only gives you the two digit year…

display dialog "" default answer short date string of (current date)
-->"9/25/04"

You can use the shell to return the four digit year like this…

display dialog "" default answer do shell script "date +%m/%d/%Y"
-->"09/25/2004"

See “man strftime” in a terminal window to read more about time formatting in the shell (Terminal.app).

No apologies, Phreak. :slight_smile:

How about just…

display dialog ((current date) as string)
-- OR -- 
display dialog (date string of (current date) as string)

j

hmmm. ok thanks everyone. this is actually for a bigger script i’m making that asks me on startup if i would like to open certain applications like itunes and safari which are things i use queite frequently. but is there a way to make it in the display dialog box without being in a text box (which is what jobu did) but still have a message above it? i’m not sure if that quite makes sense ut basically with the things greg spence said, they were all in default answer boxes and you could have a message above it. I want to have it not be in a answer box, but just as normal text in the dialog box, and have a message with it. the problem is that with the current date as a string, you don’t put quotes there when you have display dialog in front of it because you have the () instead, but when your writing something to be displayed with display dialog, you put in in between quotes “”

Will this work?

display dialog "Hello, today is... " & return & return & (current date) as string

the “& return” code forces your text onto a new line, so I used two of them to seprarte the text.

yes it worked! thanks.

Thanks, all, for helping me understand the original issue. I’ll suggest some refinements to the stated “rules” for this behavior:

date string

will be compiled as a date if string is

  1. A valid date (natch)

  2. Contains a word that is at the beginning of the name for any month. The date returned will be the first of the month. The word does not have to be an abbeviation for the month. Thus “j”, “ja”, “jan”, “janu” “januar” all compile as date “Thursday, January 1, 2004 12:00:00 AM”

  3. For the months that begin with the same letters, precedence is given in calendar order. “j” stands for January, “ju” for June, “jul” for July.

  4. If the string contains words that cannot be evaluated as months, those words will be disregarded. In “this is a test”, the first two words are discarded and “a” is compiled as date “Thursday, April 1, 2004 12:00:00 AM”

  5. If the string contains two or more words that can be evaluated as months, the first one governs. “April may call June.” compiles as April 1 “May April call June?” compiles as May 1.

but 5a) If the string begins with two words that can be evaluated as dates and if the second of those word has only one character, then a error may occur. “jan f” throws an error. “jan fe” does not.

The bottom line for me is that I’m going to have to build some kind of calendar selector because I can’t rely on the date string coercion to turn the user input string into a date.

that’s all absolutely bananas. it makes using date “string” totally useless unless you are 100% sure in advance the string contains a valid date. i mean with the first letter of the month being regnized as a month, that covers half the alphabet.

I believe that, since “jan” has already been decided upon as “January”, the date parser is taking “f” to mean “Friday”. In the absence of any numbers, the default date of the first of the month is applied. But last year, 1st January was a Thursday and this year, a Saturday. Hence the error with Friday. “Fe” can’t mean anything that the parser hasn’t already decided and hence it’s ignored. ‘date “jan f”’ will work throughout 2010 ” if the system hasn’t changed again by then. :wink: