Conversion: String-Date to AppleScript-Date

Hi,

I ask me, if it is possible to convert a date as string to an AppleScript date object!?
The string I want to convert is the NSFileModificationDate which I get from an Objetive-C Method as NSString.

I tried: set reloadCSVDate to (call method “getModificationDateForFile:” of class “methods” with parameter (myFile)) as date
I got this error: AppleScript Error: “2007-10-29 21:33:00 +0100” kann nicht in Typ date umgewandelt werden. (-1700)

Is it possible to convert this String-Date-Object into an AppleScript-Date-Object?

I would be pleased to hear from you.

Greetings, hoschy07.

Assuming that the +1 refers to time to GMT, this will do it:

set D to "2007-10-29 21:33:00 +0100" as Unicode text
set {text:U} to D as Unicode text as string -- U is D as plain text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
tell U
	set DD to text item 1 & "T" & text item 2 as «class isot» as date
	set DD to DD - (text 1 thru 3 of (text item 3) as real) * hours
end tell
set AppleScript's text item delimiters to tid
DD

Adam:
I’ve been struggling with this conversion as well.
Will your code convert strings in the format “2007-10-29 21:33:00 +0100” to the format set in System Prefs International panel?

Your sample string is what Pashua returns when you use it to get a date.
I needed a date object, so had to do quite a bit of converting. Your code sample does it in just a few lines.

Hi,

if you’re dealing with Obj-C methods anyway,
you can use this method to convert NSDate to a string depending on the international date format settings.
This string can be coerced to an AppleScript date with the date"[string]" command

- (NSString*) convertDateLocaleString:(NSDate*) theDate { [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; NSString *dateStr = [dateFormatter stringFromDate:theDate]; [dateFormatter release]; return dateStr; }

Stefan,

Thanks for replying.
But I’m AppleScript-only, I’m afraid.
I specifically wanted to know whether Adam’s sample script would produce a date in the format set in System Prefs.
But I’m also weary of using «class isot», as it’s not documented (and maybe absent in 10.5?).

Regards,
TZ

Adam’s script doesn’t work any more in Leopard,
because of the unique Unicode text class the «class isot» coercion is broken

Realize this is old, but this is what I ended up doing:

set D to "2007-10-29 21:33:00" as string
log words of D
set theDate to the current date
set year of theDate to word 1 of D
set month of theDate to word 2 of D
set day of theDate to word 3 of D
set time of theDate to ((word 4 of D) * hours + (word 5 of D) * minutes + ((word 6 of D) / 60) * minutes)
return theDate

Model: Late 2012 Mac mini
AppleScript: 2.5
Browser: Safari 537.36
Operating System: Mac OS X (10.10)

A little tersification. :cool:


set dateStr to "2007-10-29 21:33:00"
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"-", " "}}
tell dateStr's text items to set _date to (item 2 & "-" & item 3 & "-" & item 1 & " " & item 4)
set AppleScript's text item delimiters to oldTIDS
set _date to date _date


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.1 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯