About to add $(query) to a specific date

I want to add a number entered in a query to a specific date
For example;
A specific date: 2022-02-02
CHAT: !date 10
Nightbot: The day after 10 days is 2022/02/12.

Thanks for taking the time to read this article.
Even a small idea would be really appreciated. :slight_smile:

I think it might be possible.
But given !date 10 and a start date of 2022-02-02 what is the 10 meant to be added to.
Just a number of days? Could the 10 be months or years?
Date calculations are tricky at the best of times because of leap years and also time zones.
And nightbot for custom commands has a 500 character limit AFAIK.
Perhaps a urlfetch from a website that does date time calculations might be better.

I’m truly interested how you think this would be used, Someone in chat will do !date 99999999999

Hey @_K1!

You can do it by using Unix time and the fact that a day is 86,400,00 ms long: 24 x 60 x 60 x 1000.

$(eval q = parseInt('$(1)'); n = Date.now(); isNaN(q) ? 'Please enter an amount of days to add to today\'s date.' : `In ${q} days we'll be the ${new Date(n + (q * 86400000)).toISOString().split('T')[0]} UTC.`)

I worked with UTC to make things easier for me, it’s possible to switch the output to a locale value, however, you’ll have to tell me which country/timezone at the very least.

Oh god…
Your solution has been very helpful to me.
One question, I want the variable n to be able to specify a specific date (such as 2020-02-02) instead of specifying today’s date.

Assuming an user would enter the date before the amount of days to add, meaning the date would be $(1) and the amount to add $(2), you can replace Date.now() with new Date('$(1)'), with the date under the YYYY-MM-DD format (preferably, to avoid confusing the Date constructor), but then you need to turn it into Unix time before adding the days by using .getTime().

So that gives you something like this:

$(eval d = new Date('$(1)'); a = parseInt('$(2)'); isNaN(d) ? 'Please enter a valid date, preferably under the YYYY-MM-DD format' : isNaN(a) ? 'Please enter an amount of days to add to $(1).' : `${a} days from $(1) is ${new Date(d.getTime() + (a * 86400000)).toISOString().split('T')[0]} UTC.`)

Please also note that the code can substract days too.

I really appreciate your advice.
I have modified your code as follows.

!addcom !Dday
$(eval 
d = new Date('2022-10-10'); 
a = parseInt('$(1)'); 
isNaN(d) ? 'Please enter a valid date, preferably under the YYYY-MM-DD format' : isNaN(a) ? 'Please enter an amount of days to add to 2022-10-10.' : `${a} days from 2022-10-10 is ${new Date(d.getTime() + (a * 86400000)).toISOString().split('T')[0]} UTC.`)

Chat: !Dday 10
Nightbot: 10 days from 2022-10-10 is 2022-10-20 UTC.

The chat and reaction appeared as above.
To me, your advice was like rain in a drought.
I once again express my thanks.

1 Like

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