Dates, custom metadate and two AppleScripts producing different results

This post results from this thread but I felt it better to start a new thread to highlight a strange result from using two different AppleScript handlers.

In order to test the scripts you need to have a new, highlighted markdown record and a custom metadata field set to both “Type” and “Format” Date. (If you name it “Diary entry date” you should be able to test the scripts without further alteration to them.) Note that you may need to change the scripts for date order if not using the UK locale on your Mac.

Note that each script feeds a text string of Tuesday 2 March 2021 (a valid date). The first script produces the correct metadata (Tuesday 2 March 2021) . The second does not: it produces Wednesday 3 February 2021. Why?

The first script:

tell application id "DNtp"
	try
		set theSelection to the selection
		set theRecord to the content record
		set theLongDateText to "Tuesday 2 March 2021"
		set theLongDate to my formatDate(theLongDateText)
		add custom meta data theLongDate for "diaryentrydate" to theRecord
		set metaData to custom meta data of theRecord
		return metaData
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on formatDate(LongDateStr)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set the nameComponents to (text items of LongDateStr)
	set theDay to item 2 of nameComponents as integer
	set theMonth to item 3 of nameComponents as string
	set monthList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	repeat with i from 1 to the count of monthList
		if item i of monthList is theMonth then set theMonth to i
	end repeat
	set theYear to item 4 of nameComponents as integer
	set formattedDateText to theDay & "/" & theMonth & "/" & theYear as string
	set AppleScript's text item delimiters to astid
	set theDate to current date
	tell theDate to set {its day, its month, its year} to words of formattedDateText
	tell theDate to set {its hours, its minutes, its seconds} to {0, 0, 0}
	set formattedDate to theDate
	return formattedDate
end formatDate

The second script:

tell application id "DNtp"
	try
		set theSelection to the selection
		set theRecord to the content record
		set theLongDateText to "Tuesday 2 March 2021"
		set theLongDate to my formatDate(theLongDateText)
		add custom meta data theLongDate for "diaryentrydate" to theRecord
		set metaData to custom meta data of theRecord
		return metaData
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on formatDate(LongDateStr)
	set {dd, mm, yyyy} to words of (short date string of (date LongDateStr))
	set formattedDate to (dd & "/" & mm & "/" & yyyy)
	return formattedDate
end formatDate

Stephen

Is the result of formatDate of the second script actually the expected one? It doesn’t work at all over here, seems to depend on the language and/or system configuration (short date).

Well, not expected by me, certainly! From the date given it produces a date clearly the result of reversing the day and the month. In other words, the date (in both cases) is 02/03/2021—which the first script treats in accordance with the UK regional settings as 2 March but the second script decides means 3 February (which is wrong according to my regional settings).

Put another way, the first script appears properly to honour the regional settings but the second does not.

Stephen

The first one is basically hard-coded for this specific format, the second one tries to use AppleScript’s date handling which fails. Another difference is that the first formatDate function returns a date, the second one a string.

I hesitate to differ with an expert but, if that is the case, why does a custom metadata field that is set to Date accept input derived from the second script? Are you saying in effect, that I should be able to feed a date string to that custom metadata field and it should accept it?

Edit: Yes, the custom metadata Date field will accept the text date input…all my programming wasted! :grinning:

Stephen

In that case DEVONthink tries to convert the string internally to a date. That’s why I asked whether the string returned by formatDate of the second script is the right one or already wrong.

Thanks so much for that explanation: I really should have tried that at the start of the week before embarking on my little scripting exercise! Passing the text to the custom metadata Date field works perfectly and it appears that so long as it’s passed with the name of the day as part of the date (e.g., Thursday 14 October 2021 rather than merely 14/10/2021) the “2 March/3 February” problem is avoided.

I really appreciate your assistance in helping me get to the bottom of this.

Stephen

As it returns 02/03/2021 I believe it to be correct when I see it—because in my locale that refers to 2 March. However, when the custom metadata gets hold of it that date appears as 3 February 2021 (with the appropriate weekday added, of course).

In other words, it’s difficult for me to say whether it’s “already wrong” because it looks right to me before it ends up in the custom metadata field. I hope that’s (reasonably) clear!

Stephen

As if anything in the context of date handling could ever be clear :stuck_out_tongue:

Seriously, I’d suggest to refrain from local customs (as dear as they are to Brits and Germans and probably a lot of other nations) and stick to ISO dates (yyyy-mm-dd). Those are easily sorted and nobody has to bother with mentally adjusting anything at all: 2021-02-03 is always February 2nd, never March 3rd.

On a side note: why are you even including the weekday’s name in your date?

These are individual journal entries imported from Day One. They don’t contain the name of the weekday hence it’s useful to see it in the name of the record.

I don’t necessarily want the weekday name to appear in the Date custom metadata field—but I have to tolerate that for the time being. :slightly_smiling_face:

Stephen