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.


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""

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:

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)

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”

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.

You use the same code – just pass different numbers.