Convert string to date

But why should you support a wrong notated date in the first place? If someone types “31/2/99” shouldn’t he be given an error? Why should you try to fix that because he probably meant “31/1/99” or “31/3/99” and certainly didn’t meant “3/3/99”. Never trying to fix wrong inputted data into something unexpected, right?

Didn’t miss it, the given parameter was a static string :slight_smile:

Well, this particular work around a grammatical and syntactical ambiguity works for me. :slight_smile: I must say that I’m forgiving, because after all, AppleScript is a damn cute language. I love the lists, and the fact that they are made just so, that you can easily translate code from other languages back and forth, which makes it very easy to learn things in. -Also to remove recursion. :lol:

Have a good evening everyone!

That’s not MacUsrII’s issue, though. Sticking to m/d/y order, suppose the string is “31/1/2010”. That’s a valid date. But if you run your script today, current date will produce a date with a month containing only 30 days. So when you set its day to 31, it will roll over to 1. To be safe, you need to set the day to a value valid for every month before you change the month --then set the correct day.

And one more suggestion for users of Mavericks and later:

use BridgePlus : script "BridgePlus"

set listOfDates to BridgePlus's datesFromStrings:{"02/20/99"} inFormat:"M/d/y"

It requires my BridgePlus script library. Overkill for one date, perhaps, but handy if you have a bunch of them.

That is correct, and the month will be incremented as well, so it rolls over to the first of next month for instance. This is really Nigel Garveys world. It is a feature as much as a bug in other situation, where this is a great advantage during date calulations. :slight_smile:

Setting the day to 1 covers 2 cases.

The first case is when todays daynumber of a month, is greater than the month you are wanting to end up with, and you start off by setting the month

The second case is when you set a day number first, and that daynumber is greater than the possible daynumber of the current month.

Either way, it is a risk of overflow of days into next month. And if you are to calculate the probability of this happening, then you must calculate with a probability of 50% since it either happens, or don’t, and then the likelihood will be far greater than you’d intuitively expect. :confused:

And yet when I look at DJ’s code again, it’s not a problem. That’s because the maximum roll-over is three days, but he’s subtracting five days at the end – effectively making it a non-issue.

I wrote in general somewhere, higher above, on the previous page. :slight_smile:

Sorry, I missed that. AppleScript dates really are lame. I look forward to being able to use:

set df to my NSDateFormatter's new()
df's setDateFormat:"M/d/y"
set theDate to (df's dateFromString:"2/20/99") as date

Or that we all used a convention of using the iso 8601 date format: yyy-mm-dd.

But then again, the 12 month calendar is totally lame in itself, we should really change to a 13 calendar month system. The Death and Life of the 13-Month Calendar - CityLab

I see, I have updated the code to set the day first and then the month. Seems that non-English date notations (Dutch=dd/mm/yy) never have to worry about this and therefore I didn’t really saw the problem.

We use dd/mm/yy down here too, and it has the same problem. Because it only happens when testing on particular days, it’s an insidious bug.