Jump to content
C4 Forums | Control4

Date/Time Access Within Scripts


Recommended Posts

Is there a way to access or use date/time information from within scripts?  All I've found so far are the conditionals available from the Scheduler agent.  Forgive the contrived examples but this is the sort of thing I'm thinking:

  • Include current date/time pieces in a string, for any number of reasons.
  • Store the current date/time away in a variable.  Later on, extract date/time pieces to use.  For example, a keypad button announces "The recirc pump power was last cycled on Saturday Feb. 12th".
  • Do some simple date/time math.  "The recirc pump power was last cycled 4 days ago".
  • Then there's all the various postings here about calculating elapsed days, using repeating timers every x minutes to increment a variable to compute "how long", figuring out time until sunset, etc.

As I type this, I realize this is a variation on a frequent topic and there probably isn't really a solution (correct me if I'm wrong).  So, I'm turning this into a wish list for the Variable Manipulator driver from @alanchow.

  • Support DAY{} TIME{} DATE{} for the current date/time in the Create String action, like in the Advanced Announcements driver.
  • Add a new one, DATETIME{}.  It replaces with current date and time info in whatever format is reasonably easy to work against later.
  • Support some basic operations against a string PARAM (in the format returned above).  Maybe just the same DAY{} TIME{} DATE{} are available when creating a string.
  • Support some basic date/time math with two string PARAMs (or maybe one PARAM and the current date/time).  Maybe just DAYSDIFF{} and HOURSDIFF{} are available when creating a string or number.

I'm sure I've missed or oversimplified something but I think this would address my use cases.  And then my solution to the time-until-sunset problem would be something like:

Scheduler Event Every Day at Sunset
	Create SunsetString from current DATETIME{}
When your_event_here
    Create AnnouncementString "The next sunset will be in HOURSDIFF{SunsetString} hours at TIME{SunsetString}"  // should be off by less than a minute


Link to comment
Share on other sites

In thinking about it @alanchow, I have a much better idea and a simpler request for the Variable Manipulator driver.

  • Support DAY{}, TIME{}, DATE{} for the current date/time in the Create String action, like in the Advanced Announcements driver.
  • Support something like EPOCHSECONDS{}.  It would set your NUMBER variable to the number of seconds since some epoch of your choosing.

I think that's it.  A script could then stash away a pretty date/time string in any format they wanted for their own use later.  A script could also capture EPOCHSECONDS whenever they wanted and do their own math to get elapsed days, hours, mins, secs.

Link to comment
Share on other sites

  • 1 month later...
  • 4 years later...
On 7/8/2017 at 2:42 AM, alanchow said:

With the variable manipulator you can do this using os.time() and os.date().

Variable Manipulator with time
I just started playing with Variable Manipulator, and the first thing I got stock is how to get Time parameters

Screenshot 2022-03-30 192532.jpg

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...