Invalid character returned by "current date"?

I just noticed that the date-time string returned by current date has what looks like an invalid character. So, for example:

	set my_date_time to (current date) as string

That returns, for example: “Monday, 18 May 2026 at 3:34:26 pm”

The resulting string has space characters between all text items except the last. I’ve run the following to try to identify that last

set theNonSpaceCharacter to character -3 of my_date_time
set theChar to ASCII character of (ASCII number theNonSpaceCharacter)

The ASCII number returned is 63 which AppleScript says is a ?, which can’t be right.

This started happening for me on 11 August 2024 – I use date-time to name log files. I noticed for the first time today so it’s not a big issue.

I guess it’s some kind of invalid character. Is there a way to force current date to return only valid space characters ?

Hi Neophyte.

I can’t test your script immediately without changing my date/time settings. But do you get the same results if you use AppleScript’s current, Unicode-compatible terminology?

set my_date_time to (current date) as text

set theNonSpaceCharacter to character -3 of my_date_time
set theChar to character id (id of theNonSpaceCharacter)

Apple is using a narrow no-break space between the seconds and the ante/post meridiem (am/pm) characters. The UTF-8 code for a narrow no-break space is E2 80 AF.

One can replace that offending space character in an AppleScript date string by the following code:

-- replace the UTF-8 narrow no-break space with a simple space
set adate to (current date) as text
set fixedDate to do shell script "sed 's/\\xe2\\x80\\xaf/\\x20/g' <<<" & adate's quoted form

“Monday, May 18, 2026 at 4:29:18 AM”

Tested: macOS Tahoe 26.5 with Script Editor

4 Likes

Here is an ASObjC solution (using the narrow no-break space’s decimal Unicode code point value 8239) if one wishes to avoid the overhead of the do shell script command:

use framework "Foundation"
use scripting additions

set my_date_time to (current date) as text
set cleaned_date_time to ((current application's NSString's stringWithString:my_date_time)'s stringByReplacingOccurrencesOfString:(character id 8239) withString:space) as text

or a somewhat wordier Vanilla AppleScript solution:

set my_date_time to (current date) as text
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, character id 8239}
tell my_date_time's text items
	set AppleScript's text item delimiters to space
	set {cleaned_date_time, AppleScript's text item delimiters} to {it as text, tid}
end tell

or a more terse Vanilla AppleScript solution if one is certain that there is only a single narrow no-break space and that it is at neither the beginning nor the end of the string:

set my_date_time to (current date) as text
set ox to offset of character id 8239 in my_date_time
tell my_date_time to set cleaned_date_time to text 1 thru (ox - 1) & space & text (ox + 1) thru -1

Fun note, this character is also inserted into screenshots names which utilize the current date in their filenames.

Or you could just use a 24h clock, and not be bothered by this. Completely code-free.

I was curious if Shortcuts also use a narrow no-break space in a current date string, and it does. The no-break space can be replaced with a regular space with the Replace Text action (the replace character also can be the literal character). The Run Shell Script action is used to get character IDs only.

Thanks. I guess someone in Apple decided this was a good idea !!. I wonder how long ago that was. My applet adds date-time to file names and replaces spaces and special characters with underscores so was easy to add non-breaking spaces to the delimiters list.

True but, can’t say I follow w.r.t. AppleScripting.

With a 24h clock there’s no am/pm label, so no no-break space to deal with.

Do you mean if system setting is set to use 24-hour time ? That’s not an option for me as I can’t tell my users how to set up date-time on their Macs.

I usually just use an ISO 8601 date/time, e.g. (current date) as «class isot» as string. It is a standard format that uses 24-hour time, so there are no localizations or user settings to deal with - just grab and format the text however you want.

Many thanks, I would never have found or thought of that. Is there a list or way of deriving such classes ?