Problems to set creation date from the file name on import, when the date is the 31st day of the month

Hi all,

I’m on my third day as a DEVONthink user after the latest Evernote update forced me to find an alternative. Now I have a little problem that is driving me crazy.
I am migrating certain features and workflow from Evernote to DEVONthink and one of them is importing invoices, bank statements and other documents.

My problem is the following, exemplified by an invoice.
The invoice (as a pdf file) is moved to the inbox on the counter, with a name according to “Invoice Storuman Malmön 2020-01-23”.
The first part of the name is the name of the company and the second part is the invoice date.

I have created this Smart Rule to move it to the correct database, add tags and set Creation Date equal to the date from the file name.

Skärmavbild 2021-04-08 kl. 22.11.28

I use this script (I think originated from user pete31) to set the Creation Date:

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with thisRecord in theRecords
				set thisName to name without extension of thisRecord
				set theCreationDate to creation date of thisRecord
				try
					set seconds of theCreationDate to 10
					set minutes of theCreationDate to 10
					set hours of theCreationDate to 10
					set day of theCreationDate to ((characters -2 thru -1 in thisName) as string) as integer
					set month of theCreationDate to ((characters -5 thru -4 in thisName) as string) as integer
					set year of theCreationDate to ((characters -10 thru -7 in thisName) as string) as integer
					set creation date of thisRecord to theCreationDate
				end try
			end repeat

		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
end performSmartRule

This works well for all files except when the date is the 31st day of the month where the script set the date to the 1st day of the month.
For example the file Invoice Storuman Malmön 2020-01-31 get the date 2020-01-01. See screen copy below.

Skärmavbild 2021-04-08 kl. 22.18.56

Anyone who can solve this for me?

Have you tried setting the month before the day? Maybe the date format must “know” if the month has 31 days at all.

This is nothing but a wild guess, I must add.

3 Likes

Change to text -2 thru -1

Sorry, same result…

Yes! Now it works! A bit strange I must say. I guess I need to find some more information about how dates are handled. Many thanks.

There must be some kind of fail safe in the date type that prohibits dates like July 32nd or February 30th. And if the month is yet undefined it seems to be treated like a month with 30 days.

Glad I could help.

Interesting. I wonder if the data type understands leap year.

I don’t know but I do hope so. For Excel for example dates are internally just integers—January 1st 1900 is day 1 and from that date on Excel just counts the days, accurately including leap years.

Yes it does:

set d to "29-2-2020"
date d
--> date "zaterdag 29 februari 2020 00:00:00"

Excuse my dutch…

And suavito was spot-on. Set date properties from large to small, to prevent ‘rollover’.
If unsure about the day-of-the-month then FIRST set the day to 1, to prevent rollover.

1 Like

I actually read about this before but obviously didn’t pay enough attention when I wrote the script. @suavito Thanks for fixing it!

Me fixing one of your scripts, that’s a first. And almost certainly a last.

1 Like

TWO script deities in one forum :scream: Is that even allowed? :exploding_head:

1 Like

Don’t ask me, ask them!