Weird date conversion behavior

I am extracting dates from a MySQL database and have noticed something odd. The format that MySQL uses is YYYY-MM-DD, but I want to convert them to date properties. Unfortunately, AS cannot coerce it in the native form:

date "2006-06-11"

So, I put together a quick and dirty handler, and noted this:

asdate("2006-06-11")

on asdate(b)
    set x to (current date)
    set x's month to (word 2 of b)
    return x's month
end asdate

-->July

So far, it is only when the month is “02”, “04”, “06”, “09” or "11"that this happens; all those months are coerced to next month (i.e., March, May, July, October, & December instead of February, April, June, September, & November).

Interestingly, if I just swap the year to the back of the string, like this:

date "06-11-2006"

The date is then properly coerced to date “Sunday, June 11, 2006 12:00:00 AM”

I tested this on a G4 iBook and an Intel iMac, both running 10.4.8.

Hi Craig,

strange behavior, on my machine(s) it works as it should.
I tried several languages and date format settings,
but I cannot reproduce this error

Hi, Craig.

You probably ran the script yesterday “ 31st January. When you set the month of that date to June, which only has 30 days, there’s an overflow into the following month. I expect you’ll find it’s working fine this morning, 1st February.

For this sort of thing, you should either set the day of x to 1 first, or set x to some precompiled date whose day is 1. For greatest flexibility, the pre-compiled month should have 31 days too.

Right on, gentlemen. Thank you for the solution. The original handler that I wrote (I only posted a part of it) was kicking out those weird dates, and it is now clear that the reason was the order of replacement I selected: Year, Month, then Day. By reversing it to Day, Month, Year, it should work any day, any time.

. except during a short month when you want to set the date to the 31st of a long month, or during February when you want to set the date to 29th-31st of another month, or during a non-leap year when you want to set 29th February. :wink:

Hmmmm. Yes, that is an issue, isn’t it?