Saturday, January 21, 2017

#1 2007-10-29 03:37:49 pm

hoschy07
Member
Registered: 2004-04-26
Posts: 20

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.

Offline

 

#2 2007-10-29 05:07:47 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4654

Re: Conversion: String-Date to AppleScript-Date

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

Applescript:

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


iMac running OS X 10.11.6

Offline

 

#3 2008-09-20 01:30:32 pm

alastor933
Member
From: Utrecht, NL
Registered: 2008-09-12
Posts: 535

Re: Conversion: String-Date to AppleScript-Date

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.

Offline

 

#4 2008-09-20 02:22:34 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11401
Website

Re: Conversion: String-Date to AppleScript-Date

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

[code]- (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;
}[/code]

Last edited by StefanK (2008-09-20 02:23:15 pm)


regards

Stefan

Offline

 

#5 2008-09-22 05:11:15 am

alastor933
Member
From: Utrecht, NL
Registered: 2008-09-12
Posts: 535

Re: Conversion: String-Date to AppleScript-Date

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

Offline

 

#6 2008-09-22 05:16:42 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11401
Website

Re: Conversion: String-Date to AppleScript-Date

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


regards

Stefan

Offline

 

#7 2015-10-07 10:42:25 am

swbuehler
Member
Registered: 2015-10-03
Posts: 1

Re: Conversion: String-Date to AppleScript-Date

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

Applescript:

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)

Offline

 

#8 2015-11-01 06:45:33 am

ccstone
Member
Registered: 2009-02-07
Posts: 384

Re: Conversion: String-Date to AppleScript-Date

A little tersification. cool

Applescript:


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 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.037 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)