I am using Discord.js Node V12
I am currently trying to find out how to say time elapsed in the status to show how long the bot has been online. But i cannot find anyone who has asked or answered any of these questions
Well, you can use client.uptime to get the amount of time your bot has been online (since it last booted up) in milliseconds. You can then take those milliseconds and convert them into whatever unit of time you choose. Here's an example, converted to hours:
var uptime = client.uptime; //in milliseconds
var hours = uptime / 1000 / 60 / 60 ; //milliseconds -> seconds -> minutes -> hours
If you're referring to how long the bot has been up since the first time you ever started it up, that's an entirely different answer, and you would need to clarify further on that. But if you just want total online time elapsed since the bot was last offline, this is the answer.
If you are using this in a command, you can retrieve client from the message object, like so:
var uptime = message.client.uptime; //in milliseconds
var hours = uptime / 1000 / 60 / 60 ; //milliseconds -> seconds -> minutes -> hours
I do not know why you could not find the answer, if my understanding of your question is correct, because this information can be found easily on this website and on discord.js docs.
Relevant resources:
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=uptime
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=client
Related
I am working on a encryption project, and I basically want to get a different string every 5 seconds regardless from the platform it's being called from.
I can get a unique string every second, and this will give the same thing on any platform
return btoa(new Date().getSeconds().toString() + 'secret');
Every minute
return btoa(new Date().getMinutes().toString() + 'secret');
But, I want to do something similar to this but every 5 seconds, so that the other platform will have enough time (5 seconds) as 1 second is too short, and 1 minute is too long.
Any ideas?
This is not something you can find using Date, but can easily be done by using some math.
const date = new Date();
const time = date.getSeconds() - (date.getSeconds() % 5);
This will give you the seconds in this minute of a multiple of 5.
So every 5 seconds it goes from 0 to 5 to 10 and so on.
I want a certain function to run every round hour. There is the solution of running an interval when it's a round hour but I often turn on and off my script and I don't want to have to run it exactly on a round hour.
I've tried looking through some npm modules and I found one but I had some issues with it. Does anyone have a solution?
No need for javascript! You have the perfect tool for that if you use linux!
Use cron:
$ sudo crontab -e
This will open a vim editor. Then add:
0 * * * * node /execute/your/script.js
(basically, it will run your code every hour on its minute zero)
More info
cron: https://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
const HOUR = 1000 * 60 * 60;
function hourly() {
//....
setTimeout(hourly, HOUR);
}
setTimeout(hourly, HOUR - (new Date % HOUR));
Just calculate the next full hour when the server starts, and then shedule an hourly timer.
I admit that it might loose accuracy due to leap seconds :)
I am making auction app in ReactJS (JavaScript) when add product is filled with time of 3 days after submitting the form , Timer should be started in format hh:mm:ss
I dont have idea to do like this,
what are the best ways for doing this
This is how you can calculate how much time left in milliseconds, assuming that you receive creation date and days valid from server:
const creationDateMS = (new Date(creationDate)).getTime();
const timeLeftMS = (daysValid * 24 * 60 * 60 * 1000) - (Date.now() - creationDateMS);
Then use state to reload the UI. The other way could be using Moment library.
What is the best practice for saving some time informations in JS?
In my case I want to save the datetime as a user account is created. 36h after creating the account I want to show a message to the user.
Right now I'm saving a timestamp with new Date().getTime();. But maybe it is possible to calculate for some events just with new Date()?
This is how I do it, but it feels not very elegant - especially for complex calculations.
var currentTime = new Date().getTime();
if ( (timeRegistrated + 1000 * 60 * 60 * 36 - currentTime) >= 0 ) {
console.log('36h are over...');
}
I think I have to use some library for complex calculations (like current age of user or how many months between two dates)... But still the basic question: Which type of date-data should be used for the DB?
Moment.js has a fromNow function. It's way easier.
http://momentjs.com/
moment("20111031", "YYYYMMDD").fromNow();
I'm working on a busing website project and the buses run every hour. I'm having trouble creating a widget that finds the time between now and the next hour, so that it is clear when the next bus will run. My client requires that it is in javascript. Any suggestions?
Thanks in advance.
To know exactly the miliseconds from now to the next hour:
function msToNextHour() {
return 3600000 - new Date().getTime() % 3600000;
}
Please note that this will strictly tell you how many milliseconds until the NEXT hour (if you run this at 4:00:00.000 it will give you exactly one hour).
function getMinutesUntilNextHour() { return 60 - new Date().getMinutes(); }
Note that people who's system clocks are off will miss their bus. It might be better to use the server time instead of the time on the client's computer (AKA at least partly a non-client-side-javascript solution).
you have the Date object in Javascript, you could do something like:
var now = new Date();
var mins = now.getMinutes();
var secs = now.getSeconds();
var response = "it will be " + (60 - mins - 1) + " minutes and " + (60 - secs) + " seconds until the next bus";
of course you will have to work more on those calculations, but that's how you work with time in javascript
Either of the other two answers will work well, but are you aware of the docs available to you about all the other nice things date is JS can do for you?
Mozilla Date Docs
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Lots of answers, a really simple function to get the rounded minutes remaining to the next hour is:
function minsToHour() {
return 60 - Math.round(new Date() % 3.6e6 / 6e4);
}