Sunday, August 19, 2018

#1 2018-05-14 10:23:40 pm

akim
Member
Registered: 2010-04-04
Posts: 77

Converting 24 hour time to AppleScript Date

How do I convert a date string to an Applescript date that retains the correct time, as both of the  following attempts yield the same 12 am date string.

Applescript:


set DateString to "5/29/2018 11:15:00"
date DateString --> date "Tuesday, May 29, 2018 at 12:00:00 AM"

set DateString to "5/29/2018 13:15:00"
date DateString --> date "Tuesday, May 29, 2018 at 12:00:00 AM""

Offline

 

#2 2018-05-14 10:38:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5428

Re: Converting 24 hour time to AppleScript Date

akim wrote:

How do I convert a date string to an Applescript date that retains the correct time



You have to use one of the formats the user has set in System Preferences. Which makes creating dates from strings incredibly fragile, especially if used by more than one person.

You're better off building dates from the actual values:

Applescript:

on dateFrom(theYear, theMonth, theDay, theHours, theMinutes, theSeconds)
   tell (current date) to set {theDate, year, day, its month, day, time} to ¬
       {it, theYear, 1, theMonth, theDay, theHours * hours + theMinutes * minutes + theSeconds}
   return theDate
end dateFrom
set theDate to dateFrom(2018, 5, 29, 13, 0, 0)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2018-05-15 12:08:53 am

akim
Member
Registered: 2010-04-04
Posts: 77

Re: Converting 24 hour time to AppleScript Date

Thanks for your help, Shane, for showing me how to create a date and time from the current date.

Can you point me in a direction to show me what method I can use to create a future formatted date, as opposed to the current date?
My goal is to schedule a future calendar event.

I devised the the following text delimited method using a future date of  "6/29/2018 14:30:00"

Applescript:

set DateAndTimeString to "6/29/2018 14:30:00"
set AppleScript's text item delimiters to space
set {DateString, TimeString} to {text item 1, text item 2} of DateAndTimeString
set AppleScript's text item delimiters to ":"
set {hourNumber, minuteNumber} to {text item 1, text item 2} of TimeString
set AppleScript's text item delimiters to ""
set ASDate to (date (DateString)) + (hourNumber * hours) + (minuteNumber * minutes)

If you know of  a more succinct or better method, I would be interested.

Last edited by akim (2018-05-15 12:27:45 am)

Offline

 

#4 2018-05-15 12:28:21 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5428

Re: Converting 24 hour time to AppleScript Date

You use the same code -- just pass different numbers.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 
pornplaybb.com shemalevids.org macromastiavideo.com

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)