Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Reference the computer clock?

I'm working on some interactive class notes/syllabi for a class, and I wanted to pop in a little joke at the end depending on what time the student finishes everything. Is there any way to pull the time on the computer itself and then put that value in an if statement?

Thanks!

Comments

  • There's JavaScript's Date object.  What are you looking to compare?
  • Thanks. I just want to spit out a different message depending on the time. You know, the old joke of students finishing their reading late the night before. So, for instance, if it's after midnight, or after midnight the day it's due, an if statement with something like "you should really do this stuff earlier. It's late."
  • Something like this?

    <br />&lt;&lt;set $due = new Date(2014, 5, 18, 0, 0, 0)&gt;&gt;<br />&lt;&lt;if Date.now() gt $due&gt;&gt;<br />You are supposed to be done already!<br />&lt;&lt;endif&gt;&gt; <br />

    Note the weird one-off in the Date constructor (year, month, day, hour, minute, second): months start at zero, so June is 5, not 6.

    // Peter
  • cuchlann wrote:

    So, for instance, if it's after midnight, or after midnight the day it's due, an if statement with something like "you should really do this stuff earlier. It's late."


    Unless you wanted to emit a different message, then the cases "after midnight" and "after midnight the day it's due" are both handled by "after midnight".

    One way of doing that would be like so: (emits its message if the hour is between 126 AM local time)

    <<set $now = new Date()>>\
    <<if $now.getHours() gt 0 and $now.getHours() lt 6>>
    You should really do this stuff earlier. It's late.
    <<endif>>
    If you wanted to compare against a specific date and time, then something like what peterorme posted would work.  Though, I would probably do it as:

    <<set $due = new Date(2014, 5, 18, 0, 0, 0, 0)>>\
    <<if Date.now() gt $due.getTime()>>
    Oh dear! Oh dear! You shall be too late!
    <<endif>>
    Of course, you can also combine them: (all in local time)

    <<set $now = new Date(); $due = new Date(2014, 5, 18, 0, 0, 0, 0)>>\
    <<if $now.getTime() gt $due.getTime()>>/% after due date %/
    Oh dear! Oh dear! You shall be too late!
    <<elseif $now.getHours() gt 0 and $now.getHours() lt 3>>/% after 12 AM to before 3 AM %/
    You should really do this stuff earlier. It's late.
    <<elseif $now.getHours() gte 3 and $now.getHours() lt 6>>/% from 3 AM to before 6 AM %/
    Late night or early start?
    <<endif>>
  • Thanks folks! That's just perfect. : )
Sign In or Register to comment.