DarthMaul
DarthMaul

Reputation: 45

Get weekday of arbitrary date with applescript

I'm working on a script to create a table of contents. The format for each item of this toc is: weekday, month day. Example: Sunday, September 1. I have a date in the format of "09/01/2018". Can I use this date to get the weekday? When I try the following I get the error "BBEdit got an error: Can’t get weekday of date "09/01/2018"."

set theDate to theMonth & "/" & theDay & "/" & theYear --> "09/01/2018"
set theWeekday to (weekday of date theDate)

I'v also tried setting theDate "as date" to which it says "Can’t make "09/01/2018" into type date." And I've also tried adding "tell application "{BBEdit, Finder, System Events}" to set theDate..." and each one gave the same same error as above.

The only info I can seem to find when googling is how to get currentDate or some other date based on whatever "today" is. Nothing for just a random date, which is what I need.

I'm sure I'm just missing something pretty simple here, but I'm pretty new to AppleScript so any help would be greatly appreciated!

Thanks in Advance!

Upvotes: 2

Views: 1084

Answers (1)

vadian
vadian

Reputation: 285290

The most reliable (and locale independent) way is to create the date with current date and set the properties

set theMonth to 8
set theDay to 14
set theYear to 2018

set currentDate to current date
-- the first `day` avoids an overflow error 
tell currentDate to set {day, year, its month, day} to {1, theYear, theMonth, theDay}
set theWeekday to weekday of currentDate

or with a litte help from Cocoa

use framework "Foundation"

set dateComponents to current application's NSDateComponents's alloc()'s init()
dateComponents's setYear:theYear
dateComponents's setMonth:theMonth
dateComponents's setDay:theDay
set theDate to (current application's NSCalendar's currentCalendar()'s dateFromComponents:dateComponents) as date
set theWeekday to weekday of theDate

Upvotes: 2

Related Questions