"date" and "short date string" weirdness

I’d expect myNewDate to be “today”:

set myShortDate to short date string of (current date)
set myNewDate to date myShortDate

But it is weirdly not, at least on my system.

Any idea what’s going on?

It might help to show us what you do get…
System version?

2 Likes

Hi brandelune.

Have you checked your “Date format” setting (System Settings->General->Language & Region->Date format)? I can get your script to return the wrong date by having “Region” set to “United Kingdom” and “Date format” set to “8/19/25” (or "Region set to “United States” and “Date format” set to “19/08/2025”).

1 Like

But I’d expect AS to give me a “short date string” and a “date” that conform to my settings. It’s not like I’m asking it to transform anything here.

what is the value of myShortDate?

The short date string command generates a string based on the user settings that Nigel mentioned, but the date command uses AppleScript’s date determining method that has nothing to do with user settings.

For that reason I never use date string; time string or short date string commands in that way.

So would I. What I’m seeing (which you haven’t confirmed as being what you’re seeing), is probably a bug. short date string returns results in the format set under “Date format” in System Settings, whereas when date is followed by a short date string, the string’s interpreted as being in the format assumed from the “Region” setting.

1 Like

To illustrate the point made by Nigel, the region on my Sequoia computer is United States and my date format in System Settings is 8/19/25. The OP’s script correctly returns:

I changed the date format to 19/8/25 and the OP’s script incorrectly returns:

The AppleScript Language Guide states as quoted below, so either this is a bug or the date string is considered to be incomplete information.

If you construct a date using only partial information, AppleScript fills in the missing pieces with default values. The actual format is based on the settings in System Preferences.

Without some additional information from the OP, this is all pretty much guesswork.

The incomplete-information explanation appears possible:

the solution is to not rely on dates created by date string; time string or short date string to generate new dates.

Apple made a brand-new bug in date string in English localization only.

http://piyocast.com/as/archives/15953

I don’t report this bug to them because it does not occur in Japanese localization environment.
I think it would be a good idea to report it to Apple by English-speaking scripters.

1 Like

When I run the lines in an English system:

set myShortDate to short date string of (current date)
set myNewDate to date myShortDate

date "Monday, September 2, 12182 0:00:00"

As @Piyomaru just wrote, the same run in Japanese produces the expected behavior:

date "2025年2月14日 金曜日 0:00:00"

In French, I get something that is less weird than the English, but still not correct:

date "dimanche 18 août 2019 à 00:00:00"

Arabic works fine:

date "الجمعة، 14 فبراير، 2025، 00:00:00"

Hindi gets it wrong too:

date "रविवार, 18 अगस्त 2019, 00:00:00"

Just like Turkish:

date "18 Ağustos 2019 Pazar 00:00:00"

But it looks like so far there are just 2 categories of wrong:

  • 10,000 years+ very wrong (English)
  • -3 years less wrong

English:

{"2025/02/14", date "Monday, September 2, 12182 0:00:00"}

French:

{"2025/02/14", date "dimanche 18 août 2019 à 00:00:00"}

Continental Chinese

{"2025/2/14", date "2025年2月14日 星期五 0:00:00"}

FWIW, the reason for this has already been explained. For example:

The solution on an English system appears to be to use date string instead of short date string:

Another solution (but not a good one) is to set “Date format” in System Settings to the default date format for your region. For example,

Or better yet, just don’t use the OP’s approach to get a date object of the current date.

It’s weird to be called “the OP” when reading a reply to my message. It’s like you don’t reply to me, but to the crowd, but you’re really replying to me.

Also, I’m not asking for a solution, I’m stating that it’s weird and asking what is going on, considering that some languages have it working fine (even if the system is not in that language, just Script Editor).

brandelune. As you are probably aware, the term “OP” is commonly used in forums to refer to the original poster or the original post. I meant no offense.

It is not possible to answer your question with certainty, because we don’t know your Region and Date format in System Settings. However, as Nigel explained in the third post in this thread, the likely cause of the issue you note arises from the Region and Date format settings in your computer.

For example, it’s February 15th, 2025 at my location, and, just for testing, I set my Region to United States and my Date format to 19/8/25. With these settings your script in post 1 returns:

date "Monday, March 2, 2026 at 12:00:00 AM"

It’s not possible to know exactly what AppleScript is doing, but it appears to use 15 as the month, 2 as the day, and 25 as the year. Most likely, It then does some simple date arithmetic to arrive at the date noted above. However, Nigel is the forum’s expert on dates, and he may have a different or better explanation.

A question you have raised is why doesn’t date use the Date format specified in System Settings, and the likely explanation (as noted by Nigel) is that it’s a bug. As regards how this works in languages other then English, I suspect the above explanation applies, but I’ll let other forum members test that.

The following is a screenshot illustrating the test I use above.

1 Like

I think this topic’s about run its course. The way short date strings are interpreted after date appears to be the system literally taking the numbers to be the date parts it’s expecting according to the region setting, with the overflows causing the results seen. Using brandelune’s yyyy/mm/dd examples above with a dd/mm/yyyy region:

-- The short date string used in brandelune's examples in post 12.
set shortDateString to "2025/02/14"

-- In the French and UK regions, the date order's assumed to be day, month, year.
date shortDateString --> date "Sunday 18 August 2019 at 00:00:00"

-- It may work similarly to this:

-- Wrong assumptions on a ddmmyy system.
set {assumedDay, assumedMonth, assumedYear} to shortDateString's words
-- Create an AS date based on these.
tell (current date)
	set its time to 0
	set its day to 1
	-- Allow for a possible 2-digit assumed year and an assumed month possibly > 12.
	set its year to (assumedYear mod 100 + 2000) + (assumedMonth - 1) div 12
	set its month to (assumedMonth + 11) mod 12 + 1
	-- Return essentially the 2025th day of the 2nd month of (20)14.
	return it + (assumedDay - 1) * days --> date "Sunday 18 August 2019 at 00:00:00"
end tell

Edit: Year and month calculations corrected to allow for when assumedMonth is divisible by 12.

2 Likes

The problem is that AppleScript can convert text to dates in a very limited number of date formats.

But it generates the short date string based on how the user has configured date strings in the settings, and those can easily be configured in a way that is not compatible with appleScript’s text to date conversion.

AppleScript has no mechanism to change its text to date conversion based on how the user has configured the short date string.

This makes using short date string unreliable as a way to store and convert dates.

This has been an issue with AppleScript and dates since the start, and it’s not likely to change anytime soon.

Then perhaps the most reliable way is to use /bin/date?

-- current date in US English format 
do shell script "LC_ALL=en_US /bin/date"
-- current date in Japanese format
do shell script "LC_ALL=ja_JP /bin/date"
-- date in user-specified format, e.g. year/month/day, day name in US English
do shell script "LC_ALL=en_US /bin/date '+%Y/%m/%d, %A'"

List of possible date formats can be found in Mac OS X Manual Page For strftime(3)

1 Like

Those are good ways to generate a date string, but they face the same issue, they cannot be accurately converted to date using applescript’s text to date.

set firstDate to do shell script "LC_ALL=en_US /bin/date"
{date firstDate, firstDate}
--fail {date "Monday, February 15, 2010 at 12:00:00 AM", "Sat Feb 15 10:33:56 PST 2025"}
set jpDate to do shell script "LC_ALL=ja_JP /bin/date"
--{date jpDate, jpDate}
--fail jpDate not recognized
set userDate to do shell script "LC_ALL=en_US /bin/date '+%Y/%m/%d, %A'"
{date userDate, userDate}
--{date "Tuesday, September 2, 12183 at 12:00:00 AM", "2025/02/15, Saturday"}

It’s possible to code your own, if you’re desperate. :wink:

set shortDateString to "2025-02-15" -- Short date as per user's "Date format" setting.
shortDateToASDate(shortDateString)

on shortDateToASDate(shortDateString)
	tell (current date)
		set currentYear to (its year)
		set {its day, its month, its year, its time} to {3, 2, 2001, 0}
		set pattern to its short date string
		set ymd to {missing value, missing value, missing value}
		repeat with i from 1 to 3
			set ymd's item ((pattern's word i) mod 10) to shortDateString's word i as integer
		end repeat
		set shortDateYear to ymd's beginning
		if (shortDateYear < 100) then
			set currentYearLo to currentYear mod 100
			-- If expanding an abbreviated short date year and the abbreviated and current years
			-- are both close to century boundaries, use the solution nearest to the current year.
			if (shortDateYear < 5) then
				if (currentYearLo > 94) then set shortDateYear to shortDateYear + 100
			else if (shortDateYear > 94) then
				if (currentYearLo < 5) then set shortDateYear to shortDateYear - 100
			end if
			set ymd's item 1 to currentYear - currentYearLo + shortDateYear
		end if
		set {its year, its month, its day} to ymd
		return it
	end tell
end shortDateToASDate
1 Like