Convert string to date

I’m trying to convert sting to date as per below

tell application "Finder"
	set dateString to "02/20/99"
	
	set thisDate to date dateString
	set fiveDaysEarlier to thisDate - 5 * days
end tell

and I’m getting below error:

Any idea where I made mistake.

It will probably work when you get rid of the tell application block. However, if a string to date coercion succeeds depends on your system preferences.

Here another approach which I prefe that doesn’t rely on any date formatting from the system:

--edit
set dateString to "02/20/99"
set theDate to current date
tell theDate to set {it's month, it's day, it's year} to words of dateString
tell theDate to set its time to 0
theDate
--> date "vrijdag 22 februari 0099 00:00:00"

Oops…
That date is outside the legal range, hence the error with the day, presumably.
You have to use a 4-digit year.

Hello.

There is a problem with this, that say if todays day is the 30th of any month but february, and you want to set the date to february the 4th, then when you set the month, it will overflow into mars, and then the day will be set to 4, and you’ll end up with 4th of mars.

Try this:

set today to (current date)
set month of today to January
set day of today to 30

copy today to altered_date
set month of altered_date to February

A better approach, where we set the day to 1 for starters, to avert overflow, caused by an unfortunate combination of current date and the date string specified.

set dateString to "02/20/1999"
set theDate to current date
-- sets the day to 1 to avert overflow
tell theDate to set {its day, it's month, it's day, it's year} to {1} & words of dateString
tell theDate to set {it's hours, it's minutes, it's seconds} to {0, 0, 0}
set fiveDaysEarlier to theDate - 5 * days

To answer TS’s question and to have it work with cc instead of cccc format:


set dateString to "3/3/99"
set theDate to current date
set currentYear to year of theDate
set {theMonth, theDay, theYear} to words of dateString
set theYear to theYear as integer
if theYear > currentYear mod 100 then
	set theYear to theYear mod 100 + (currentYear div 100 - 1) * 100
else
	set theYear to theYear mod 100 + currentYear div 100 * 100
end if
tell theDate to set {it's day, it's month, it's day, it's year} to {1, theMonth, theDay, theYear}
tell theDate to set {it's hours, it's minutes, it's seconds} to {0, 0, 0}
set fiveDaysEarlier to theDate - 5 * days

I don’t follow, we’re going back in time. So the 30th will be set back to the 25th not the next month.

Hello.

I was referring to the first part of it that sets a date based on a date string.

We must always set the day to 1 for starters, when we set the day/month of a date, in order to avoid any overflow with febrary, and any other months containing possibly less days than the current date of the date object we are changing the properties of (like every month containing 30 days, if the day of the date object we are changing is the 31st).

Of course it doesn’t really matter in a “one off” solution, so then please regard it as a general comment. And I believe the algorithm for converting a twodigit year into a four digit year, is that if the two digit year is less than or equal to 38, then the century should be 2000, 1900 otherwise. :slight_smile:

Hello

Since when are we allowed to use set it’s year to theYear ?

My understanding was that the correct spelling was:
set its year to theYear.

Aren’t we supposed to use the possessive pronoun “its”, not the shortened form of “it is” ?

About the year, DJ’s scheme doesn’t match AppleScript standard behaviour:

if theYear is less than 10, AS leave the year to the passed number ( 11/11/8 → 11/11/8)
if theYear is greater than 9, AS set the year to 2000 + theYear (11/11/10 → 11/11/2010)

But from my point of view, breaking the standard is not a bad idea here and honestly I wonder what is the logic behind the standard behavior.

Yvan KOENIG (VALLAURIS, France) vendredi 12 juin 2015 14:59:10

I believe it’s always been possible to use it’s as a synonym for its, since it’s is the grammatically correct version, its is accepted because of us aliens. :slight_smile: Mere speculations of course, apart from it’s always been a legal possessive pronoun.

Thanks for the heads up, I wasn’t aware of this weird behavior.

People are pleased with that in the year 2037 :). In the past they have chosen 75 and other cutoffs and it’s not really standardized. My personal favor is, for timestamps, to support the longest backward compatible date, so the software could be used like forever. Because timestamps can only be a date in the past, never a date in the future.

its is listed in the reserved words.
In the Language guide, we may read : Coercing an integer to a number does not change its class
tell script “Counter” to log its nextNumber() – logs “1”
tell script “Counter” to log its nextNumber() – logs “2” which are the correct spelling.

We never see :
Coercing an integer to a number does not change it’s class
tell script “Counter” to log it’s nextNumber() – logs “1”
tell script “Counter” to log it’s nextNumber() – logs “2”

whose logical meaning would be :
Coercing an integer to a number does not change it is class
tell script “Counter” to log it is nextNumber() – logs “1”
tell script “Counter” to log it is nextNumber() – logs “2”

Yvan KOENIG (VALLAURIS, France) vendredi 12 juin 2015 15:15:37

Hello.

@ DJ Bazzie Wazzie:

I like the 38, but I think it should have been adjusted by 1 year from 2000, but it is reasonable choice, because then the next date constraint/bug, kicks in. That is the one algorithm I have seen in use here and there, but there are probably other solutions as well. I mentioned that one, because that is the one I have seen most often.

I agree with you that full timestamps in general as the best way to store/specify a date.

@Yvan Koenig

I swear I have seen it here since 2006. Bruce Phillips uses it in this post from 2007, where in all fairness Nigel Garvey states that the pronoun its doesn’t contain an apostrophe in English, which I believe bluntly to be correct.

If dateString is set to “2/31/99” the oddity described by McUsr would
give:
theDate = 1999-03-03
fiveDaysEarlier = 1999-02-26

Honestly, I don’t know if the final date is wrong as we were asking to move back from five days starting from a date which doesn’t exists.

Of course we may wish that the script would correct the wrong original date but what would be the corrected one : 2/29/99 or 3/31/99 ? :wink:

Yvan KOENIG (VALLAURIS, France) vendredi 12 juin 2015 15:27:42

@ McUsr

it’s is not a variant of the possessive pronoun which, as far as I know, is spelled its in English from USA as well as in English from Great Britain , it’s is a shortened version of “it is”.

Yvan KOENIG (VALLAURIS, France) vendredi 12 juin 2015 15:31:27

Not in AppleScript. :slight_smile:

It’s because we often refer to an objects property by tacking on an apostrophe to it, to indicate that it possess a property, instead of using “of” with the property, and, in a tell block, we use it to refer to the object, and then we tack the apostrophe and the s onto it accordingly, to indicate the same possessiveness as that of a named object, when we don’t use “of”.

The most confusing way to getting a property in my opinion by the way is

set {day:thisday} to this_date

. :slight_smile:

Edit

I did edit the last comment, because in an errounous moment I remembered it as a way of setting, not getting a property from an object.

Agree, in a better situation the date should return an error that the date is not valid rather than trying to solve the problem.

It fails down here, because of the date formatting of the system :wink:

See my second post :slight_smile:

Sadly, this is so.

I suspect it’s accepted because doing otherwise would require an exception to the 's rule. But I can assure you, English-speakers sadly use the wretched wrong form regularly. The phenomenon known as the errant apostrophe is, alas, widespread.

@ McUsr

It is not, even in AppleScript.

I guess that the engineers just knew that we will never have to write “it is” in an instruction so they decided to accept the wrongly spelled form.

set it is year to theYear means nothing.
According to the list of reserved words, the official syntax would be : set its year to theYear
and as accepting the wrong spelling set it’s year to theYear can’t hurt they didn’t reject it.

I was surprised because if I type:
if aa doesn’t equal 10 then set aa to 10
which is correctly spelled, the interpreter replace it by
if aa is not equal to 10 then set aa to 10

From my point of view it would have been logical to replace it’s by its automatically.

As I am curious, I saved the script :

set it's year to theYear

and the script :

set its year to theYear

Their datafork are identical.
The differences are only in their resource forks which as far as I know means that they are identical in terms of functionality but differ in aspect.

Yvan KOENIG (VALLAURIS, France) vendredi 12 juin 2015 16:07:20

You’re still missing MacUsrII point – if you pass a day of 31, and you do it when you system clock is showing a month with fewer than 31 days, you will get an error. You need to set the day to 1 (or less than 29, anyway) first, and then set the real day later. To quote a Nigel one-liner:

tell (current date) to set {theASDate, year, day, its month, day, time} to {it, theYear, 1, theMonth, theDay, theHour * hours + theMinute * minutes + theSeconds}

And you missed my point – if you don’t consult the system, you won’t realise that some of us use d/m/y order :wink: