Page 1 of 1

An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 7:42 am
by chobo
Hi all,

Just upgraded to 7.4 and have had pretty good success converting some other scripts. This one not so much...

Code: Select all
tell application "IndigoServer"
set today to current date
set weekdaytoday to the weekday of today
set value of variable "Day_of_week" to weekdaytoday
end tell

Thoughts?

Re: An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 8:26 am
by Grognard
This should work:

Code: Select all
from datetime import datetime, time
day_of_week = datetime.today().strftime("%A")

Re: An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 8:32 am
by forestfield
This should do it...

Code: Select all
import datetime

# create a list of days
days = [u'Monday', u'Tuesday', u'Wednesday', u'Thursday', u'Friday', u'Saturday', u'Sunday']
# choose today from that list
today = days[datetime.datetime.today().weekday()]

# create the indigo variable 'Day_of_Week', don't panic if to already exists
try:
  indigo.variable.create('Day_of_Week', value='')
except NameNotUniqueError:
  pass

# set 'Day_of_Week' to today
indigo.variable.updateValue(indigo.variables['Day_of_Week'].id, value=today)

Re: An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 8:35 am
by forestfield
Beaten to it,
and I forgot about strftime('%A') - may reuse that elsewhere

Re: An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 4:03 pm
by DaveL17
Just an FYI, you can also use the Python format specifier to turn datetime objects into formatted strings.

Code: Select all
now = indigo.server.getTime()
indigo.variable.updateValue("Day_of_week", u"{0:%A}".format(now))

Re: An easy one: Day of the Week to Variable

PostPosted: Sun Oct 20, 2019 5:39 pm
by chobo
Thanks, Grognard, forestfield, & DaveL17! This one is resolved!