Adding X minutes to a timestamp for countdown

I set up a command that logs a timestamp in a list API retrievable via a urlfetch. I want to use that timestamp as the basis for both a countup and a countdown timer with Nightbot to return both timers in a single command. The amount of time I want to add to the timestamp is static [90 minutes]. (The idea is to say “we started Xtime ago and have Ytime left” | X being the original timestamp with countup and Y having an additional 90 minutes tacked on with countdown)

I can’t quite get any JS working to add the time. The timestamp is formatted as: MMM Do YYYY HH:mm:ss | I would settle for establishing a second timestamp to store the 90 minute later timestamp, but I don’t think that makes a difference. Can anyone here provide some guidance on combining a countdown command with an embedded urlfetch to retrive the timestamp and adding 90 minutes to that?

Thanks in advance…

Hey @abomination!

Nightbot’s $(countup) and $(countdown) work best with time formatted as follows: MM DD YYYY HH:mm:ss, therefore this is the format I’ll use, but if you really want to keep using your format, the same idea applies.

Basically, what we have to do is convert the hours and minutes to minutes, add 90 minutes and convert the whole thing back to hours and minutes:

$(eval t=`$(urlfetch LINK)`.match(/\d+/g); m=t[3]*60+t[4]+90; t[3]=Math.round(m/60); t[4]=Math.round(m%60); $(countdown ${t[0]} ${t[1]} ${t[2]} ${t[3]}:${t[4]}:${t[5]} TIMEZONE)) 

You may encounter issues when you’re between 0 and 90 minutes from a new day, but that should be about it.

.match(/\d+/g) — matches all the numbers and put them in an array, given that the format is
MM DD YYYY HH:mm:ss, and that each of them are numbers we’ll have:
t[0] = MM
t[1] = DD
t[2] = YYYY
t[3] = HH
t[4] = mm
t[5] = ss

Thanks @Emily! I am going to give this a whirl.
After a lot of trial and error I managed to do the “simple” task of creating a second timestamp 90 minutes ahead of now but the ultimate goal was working with a single timestamp instead of two. Changing the format should be no problem at all. I don’t recall what led me to use the one I was, I thought it was something from the Nightbot docs.

JS and I don’t get along well. Regex won’t return my calls either. :smile:

After a lot of trial and error I managed to do the “simple ” task of creating a second timestamp 90 minutes ahead of now but the ultimate goal was working with a single timestamp instead of two.

Oh right, that’s my bad, it was late and my brain was foggy, I forgot that Nightbot will execute/replace all the variables before the code executes, so it calls $(countdown) before it computes t. The solution would be to use two commands, the second being the alias of the first one:

$(eval t=`$(urlfetch LINK)`.match(/\d+/g); m=t[3]*60+t[4]+90; t[3]=Math.round(m/60); t[4]=Math.round(m%60); `${t[0]} ${t[1]} ${t[2]} ${t[3]}:${t[4]}:${t[5]} TIMEZONE`)

$(countdown $(query))

But since it’s on a timer with a $(countup) as well, the solution would be something like this:

$(eval t=`$(urlfetch LINK)`; a=t.match(/\d+/g); m=a[3]*60+a[4]+90; a[3]=Math.round(m/60); a[4]=Math.round(m%60); `${t} ${a[0]} ${a[1]} ${a[2]} ${a[3]}:${a[4]}:${a[5]} TIMEZONE`)

And then have the timer call an alias command with that code in it:

We started $(countup $(1) $(2) $(3) $(4) $(9)) ago, and we have $(countdown $(5) $(6) $(7) $(8) $(9)) left.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.