Convert date problem

Hi,

I have this date as text and it’s like : “6Apr200918:11:58” → I can easily switch places with “6”, “Apr”, “2009” and “18:11:58”

And I’d like to compare such a date with the current date → (current date) - theDate to see how much time has passed (not only days but also the minutes etc.)

I can convert it a little bit with text items a thru b but with that I’m not much further, I’ve made “06/04/09 18:11:58” out of it … (dd/mm/yy and then hh/mm/ss)

But I can’t compare with that right ? Any other ideas ?

Thanks

Hi,

the proper class to handle date math is the class date.
Consider, that in AppleScript the date string format depends on the international date format settings

assuming the time string of theDate is always hh:mm:ss, the month is always a three letter abbreviation and the year is always yyyy, this is quite independent of international date format settings


property monthList : "JanFebMarAprMaiJunJulAugSepOktNovDec"

set theDate to "6Apr200918:11:58"
tell theDate to set {dy, mn, yr, ti} to {text 1 thru -16, text -15 thru -13, text -12 thru -9, text -8 thru -1}
set monthInteger to ((offset of mn in monthList) - 1) / 3 + 1 as integer
set myDate to (date ti)
tell myDate to set {its year, its month, its day} to {yr, monthInteger, dy}

set diffDate to (current date) - myDate -- difference in seconds

Please check monthList and change it to your appropriate abbreviations

Thanks Stefan, works like a charm. My main problem was the day, it could be either 6 or 16 or 26… but it works with your code… I guess I’ll go for property monthList : “JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC” because the date is generated by a unix command and thus will be the same format on all systems. (I hope so, I’ll have to check some stuff before I’m sure)

Thanks again!

the script considers both one-digit or two-digit day values

Again, if this script’s run on a date whose day number’s higher than the number of days in theDate’s month, the result will be a month out. Set myDate’s day to a safe value (between 1 and 28) before changing the other values, to avoid the possibility of month overflow:

property monthList : "JanFebMarAprMayJunJulAugSepOctNovDec"

set theDate to "6Feb200918:11:58" -- Date in a month with only 28 days.
tell theDate to set {dy, mn, yr, ti} to {text 1 thru -16, text -15 thru -13, text -12 thru -9, text -8 thru -1}
set monthInteger to (offset of mn in monthList) div 3 + 1
set myDate to (date ti) -- Today's date. Might be 30th or 31st of something.
tell myDate to set {its day, its year, its month, its day} to {1, yr, monthInteger, dy} -- Safe day, year, month, required day

set diffDate to (current date) - myDate -- difference in seconds

Or start from a known safe date:

property monthList : "JanFebMarAprMayJunJulAugSepOctNovDec"

set theDate to "6Feb200918:11:58" -- Date in a month with only 28 days.
tell theDate to set {dy, mn, yr, ti} to {text 1 thru -16, text -15 thru -13, text -12 thru -9, (text -8 thru -7) * hours + (text -5 thru -4) * minutes + (text -2 thru -1)}
set monthInteger to (offset of mn in monthList) div 3 + 1
set myDate to date "Wednesday 1 January 1000 00:00:00" -- Known safe date that won't overflow whatever theDate's month.
tell myDate to set {its year, its month, its day, its time} to {yr, monthInteger, dy, ti}

set diffDate to (current date) - myDate -- difference in seconds

PS. The date subtraction at the end doesn’t take standard and daylight-saving time into account, if that’s important.

Didn’t notice that yet… But because the dates are generated by a UNIX command, I guess they’re always correct and never be like 31 February or something like that. And nevermind about DST…
But I’ll certainly take a look at your script Nigel. Thanks

There is an error in both Nigel scripts.

The instruction

tell myDate to set {its day, its year, its month, its day} to {1, yr, mn, dy} -- Safe day, year, month, required day

must be:

tell myDate to set {its day, its year, its month, its day} to {1, yr, monthInteger, dy} -- Safe day, year, month, required day

At least it must be edited this way for macOS X 10.4.11.

Yvan KOENIG (from FRANCE jeudi 9 avril 2009 11:14:50)

Thanks Nigel, I always forget this :wink:

Nigels corrected script give me “300374” seconds for “6Apr200918:11:58” and Stefans “234870” for the exact same time. - I ran the script and the exact difference is 65518 seconds. The results above differ a few seconds but 65518 is exact.

Tests verify that there’s something wrong with Nigel’s script. For a real time of ~47 seconds it gave 41463. But Stefans script has problems with the 29-30-31 days of the month like Nigel said.

Thanks, Yvan. You’re absolutely right. I was working with my Jaguar system, which doesn’t allow month setting by number, so I’d been hard-coding month constants to test the results. I reinstated the wrong variable when I came to post the script. I’ve now corrected it above.

Sorry. That’s probably due to the error Yvan identified. My corrected corrected script gives me a result slightly less than what Stefan’s gives you, which would be accounted for by our being in different time zones.

It was Stefan’s AppleScript code that might have tried to generate 31st February. If you set the day of an AppleScript date to more than the number of days in its month, or you set the month of a date to a month that doesn’t have enough days to cover its existing day, the date overflows into the following month, as in the following demo:

-- Supposing the script is run on 31st May 2009 and theDate is "6Feb200918:11:58"

-- ti will be returned as "18:11:58" and monthInteger will be calculated as 2.
-- Lets hard-code these for the moment.
set ti to "18:11:58"
set monthInteger to 2

set myDate to (date ti)
-- Only the time is provided here, so AppleScript takes the rest of the details from the current date.
--> date "Sunday 31 May 2009 18:11:58" -- Or however this looks with your date/time preferences. So:
set myDate to date "Sunday 31 May 2009 18:11:58"

tell myDate to set its month to monthInteger
myDate
--> date "Tuesday 3 March 2009 18:11:58"

-- Setting the day to 6 will now give 6th March instead of 6th February.

Huh ? So you think it’s normal that there’s a difference of the 65000 seconds. Both ran on my Mac ?

On my machine, both my versions of the script (as corrected above) give exactly the same results as Stefan’s, except under the previously discussed circumstances where mine and Stefan’s differ by a calendar month.

The 65518 seconds you mentioned in an earlier post correspond to the time “18:11:18” that we’ve been using in theDate. I can’t see any reason why that should be added twice by either of my versions, but not by Stefan’s, when run on anyone else’s machine. :confused:

Maybe the problem lies in

tell myDate to set {its day, its year, its month, its day} to {1, yr, monthInteger, dy}

The time (ti) isn’t added ? which is the missing 65518 seconds.
I tried

tell myDate to set {its time, its year, its month, its day} to {ti, yr, monthInteger, dy}

because I didn’t knew what the “1” was for, why setting its day to 1 and then set it to the real date ? doesn’t make sense to me… - That’s probably me not understanding that piece of the script :).
So I replace the first “its day” with “its time” and set the “1” to “ti”… which didn’t work :confused:

Any help?

this is the correct one


property monthList : "JanFebMarAprMayJunJulAugSepOctNovDec"

set theDate to "6Apr200918:11:58"
tell theDate to set {dy, mn, yr, ti} to {text 1 thru -16, text -15 thru -13, text -12 thru -9, text -8 thru -1}
set monthInteger to (offset of mn in monthList) div 3 + 1
set myDate to (date ti) -- Today's date. Might be 30th or 31st of something.
tell myDate to set {its day, its year, its month, its day} to {1, yr, monthInteger, dy} -- Safe day, year, month, required day

set diffDate to (current date) - myDate -- difference in seconds

thanks