i tried to use primefaces extension timer...Let's say i set timeout is 172439221...
When I user format HH:mm:ss the timer cannot display 47:53:59...
So i try to create javascript function like this
<script type="text/javascript">
function formatInterval(value) {
var diffSeconds = value / 1000 % 60;
var diffMinutes = value / (60 * 1000) % 60;
var diffHours = value / (60 * 60 * 1000) % 24;
var diffDays = value / (24 * 60 * 60 * 1000);
if (diffDays > 0) {
diffHours = diffHours + (diffDays * 24);
}
return Math.floor(diffHours) + ":" + Math.floor(diffMinutes) + ":" + Math.floor(diffSeconds);
}</script>
and the timer like this
<pe:timer
update=":globalMessages createOrderButton" formatFunction="return formatInterval(value);"
timeout="#{orderPendingBacking.calculateCountDownTimeNoLimit(pending.orderTime)}"
listener="#{orderPendingBacking.onTimeout()}"/>
But the output is like this
The timer is wrong...it should be 47:53:59, and the timer didn't count down...
Please help me...thx
I change my javascript code like this
<script type="text/javascript">
function formatInterval(value) {
var diffSeconds = value / 1000 % 60;
var diffMinutes = value / (60 * 1000) % 60;
var diffHours = value / (60 * 60 * 1000) % 24;
var diffDays = value / (24 * 60 * 60 * 1000);
var days = Math.floor(diffDays);
var hours = Math.floor(diffHours);
if (days > 0) {
hours = hours + (days * 24);
}
return hours + ":" + Math.floor(diffMinutes) + ":" + Math.floor(diffSeconds);
}
</script>
Now the timer show correct value 47:53:59...But the timer still not count down
Related
Can someone please help me out with this? I'm getting an error 'count is not defined' when executing the below code. I'm trying to create a countdown clock in node.js. If I log the timer variable inside the function it works but outside of the function, it is undefined.
const getOrder = async(request, response) => {
const data = await knex("orderbook_table").select()
const time_data = await knex("orderbook_table").select("timestamp")
console.log(time_data)
var timer;
setInterval(function count() {
for (var i = 0; i < time_data.length; i++) {
//console.log(tableRows[i]);
var entryDate = time_data[i].timestamp;
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24;
countDown = new Date(entryDate).getTime();
let now = new Date().getTime();
distance = countDown - now;
days = Math.floor(distance / (day));
hours = Math.floor((distance % (day)) / (hour));
minutes = Math.floor((distance % (hour)) / (minute));
seconds = Math.floor((distance % (minute)) / second);
timer = days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s";
//console.log(timer);
}
}, 1000);
var orderStr = data;
count();
console.log(timer);
response.send({ orderStr });
//refresh()
The count function is defined and scoped under the setTimer, you should create the function as callback outside of setInterval then call within that setInterval the created function
You can give a look to this previously answered thread or to the documentation
I made a 5 minute(300 seconds) cooldown for my discord.js bot, but when someone uses it more than once in under 5 minutes, it sends something like:
#Edward, please wait 250.32 second(s) until you can use this command!
Is there any way to change 250.32 seconds to something like 4 minutes 10 seconds or something close to that? I'm a noob at Node.js so help would be greatly appreciated.
if (!cooldowns.has(command)) {
cooldowns.set(command, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command);
const cooldownAmount = 1 * 300 * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command}\` command.`);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
Read this discussion.
Basically you need to divide seconds with 60 to get minutes and then you use % to get the reminder:
const cooldownAmount = 1 * 275.45 * 1000;
const timeLeft = cooldownAmount / 1000;
var quotient = Math.floor(timeLeft / 60); //minutes
var remainder = Math.floor(timeLeft % 60); //seconds
console.log(quotient); // 4
console.log(remainder); // 35
This code should work in your case:
if (!cooldowns.has(command)) {
cooldowns.set(command, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command);
const cooldownAmount = 1 * 300 * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
var quotient = Math.floor(timeLeft / 60); //minutes
var remainder = Math.floor(timeLeft % 60); //seconds
return message.reply(`Please wait ${quotient} minutes and ${remainder} seconds before reusing the \`${command}\` command.`);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
I'm trying to create an array of time durations in a day with 15 minute intervals with moment.js and ES6.
Example: let time_duration= ["15 Min", "30 Min",..., "1 hr 15 min", "1 hr 30 min"]
I think it supposed to work:
generateTimeDurations(minutesGap: number, length: number): string[] {
const result = Array(length).fill(null);
let acc = 0;
return result.map(_ => {
acc += minutesGap;
if (acc >= 60) {
return `${Math.floor(acc / 60)} hr ${acc % 60} min`;
} else {
return `${acc} min`;
}
});
}
If you only need static time levels like "15 Min", "30 Min" and so on you could try to do it in plain JS with Array#fill and Array#map.
const hours = 24;
const minutes = 24 * 60;
function minsToHM(totalMins) {
let padZero = value => String(value).padStart(2, "0");
totalMins = Number(totalMins);
const h = Math.floor(totalMins / 60);
const m = Math.floor(totalMins % 60);
const hDisplay = h > 0 ? padZero(h) + (h == 1 ? " Hour" : " Hours") : "";
const mDisplay = m > 0 ? padZero(m) + (m == 1 ? " Min" : " Mins") : "";
return `${hDisplay}${h > 0 && m > 0 ? ", ": ""}${mDisplay}`;
}
function splitHours(hours, difference) {
const mins = hours * 60;
return Array(Math.floor(mins / difference))
.fill(1)
.map((_, idx) => (idx+1) * difference)
}
const output = splitHours(1.5, 15).map(i => minsToHM(i))
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0px }
console.clear();
function digitToMinute(digit) {
return digit * 60 / 100;
}
function minutesToDigit(minute) {
return minute * 100 / 60;
}
function digitsToHourAndMinutes(digit) {
return [Math.floor(digit / 60), digitToMinute(getFraction(digit / 60))];
}
function getFraction(f) {
return Math.ceil(((f < 1.0) ? f : (f % Math.floor(f))) * 100)
}
function getIntervals(max) {
var intervals = [];
var span = 15;
var i = 0;
while (i <= max) {
if (i > 0) {
let [hour, minute] = digitsToHourAndMinutes((i));
let string = [];
if (hour > 0) {
string.push(hour + ' hour');
}
if (minute > 0) {
string.push(minute + ' minutes');
}
intervals.push(string.join(' '));
}
i = i + span;
}
return intervals;
}
console.log(getIntervals(60 * 5));
I am trying to print dates by adding more days to them by using nlapiAddDays() in a loop
var startdate = nlapiGetLineItemValue('item','custcol_startdt',i+1);
var enddate = nlapiGetLineItemValue('item','custcol_enddt',i+1);
var stringtodate1 = nlapiStringToDate(startdate);
var stringtodate2 = nlapiStringToDate(enddate);
//BELOW FORMULA I USED TO GET NUMBER OF MONTHS BETWEEN TWO DATES
var diff =(stringtodate2.getTime() - stringtodate1.getTime()) / 1000;
var diffMonths = Math.ceil(diff / (60 * 60 * 24 * 7 * 4)-1);
if(diffMonths > 1)
{
for(i=0; i <= diffMonths; i++)
{ var AddDays = 30;
var days = null;
if(i == 0)
{
days = stringtodate1;
}
else
{
days = nlapiAddDays(stringtodate1, AddDays);
}
}
nlapiLogExecution('DEBUG','days',days);
AddDays++;
}
here,if i becomes 1,30 days are added but when i becomes 2, 60 days should get added because of AddDays++ but it is not happening.30 days are added for every loop.
You are resetting AddDays to 30 at the top of the loop. You would need:
var AddDays = 30;
for(i=0; i <= diffMonths; i++)
{ ...
I need to calculate the datetime difference using the node.js. I am using the dateformat package to get current datetime format. My code is below:
var dateFormat = require('dateformat');
var prev_date=2017-08-09T19:00:58+0530;
var now = new Date();
var current_date=dateFormat(now, "isoDateTime");
Here I have already the previous date and also the current datetime also. I need to calculate the difference between two dates in hour, minute and second separately.
let firstDate = new Date("7/13/2016"),
secondDate = new Date("09/15/2017"),
timeDifference = Math.abs(secondDate.getTime() - firstDate.getTime());
console.log(timeDifference);
alert(timeDifference)
If you want the number of days between the two dates...
let differentDays = Math.ceil(timeDifference / (1000 * 3600 * 24));
console.log(differentDays);
alert(differentDays);
You could also use Moment.js https://momentjs.com/docs/
console.log(moment.utc(moment(firstDate,"DD/MM/YYYY HH:mm:ss").diff(moment(secondDate,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss"))
If you want to calculate day-difference between two dates you don't require any package. You can use simple JavaScript.
var date1 = new Date("08/09/2017");
var date2 = new Date("08/10/2017");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24)); //gives day difference
//one_day means 1000*60*60*24
//one_hour means 1000*60*60
//one_minute means 1000*60
//one_second means 1000
console.log(diffDays)
function date_diff(d1, d2, get_item)
{
var date1 = new Date(d1)
var date2 = new Date(d2)
var Difference_In_Time = date1.getTime() - date2.getTime();
switch (get_item) {
case 'month':
return Math.round(Difference_In_Time / (1000 * 3600 * 24 * 30));
case 'day':
return Math.round(Difference_In_Time / (1000 * 3600 * 24));
case 'hour':
return Math.round(Difference_In_Time / (1000 * 3600));
case 'minute':
return Math.round(Difference_In_Time / (1000 * 60));
case 'second':
return Math.round(Difference_In_Time / 1000);
default:
break;
}
}
var moment = require('moment');
diffDateAndTime(stratDate, endDate, startTime, endTime) {
let nowDate = moment(Date.now()).format("YYYY-MM-DD");
let nowTime = moment(Date.now()).format("YYYY-MM-DD HH:mm:ss");
let dateOfStart = moment(stratDate).format("YYYY-MM-DD");
let dateOfEnd =moment(endDate).format("YYYY-MM-DD");
let timeOfStart = moment(Date.now()).format("YYYY-MM-DD") + " " + moment(startTime).format("HH:mm");
let timeOfEnd = moment(Date.now()).format("YYYY-MM-DD") + " " + moment(endTime).format("HH:mm");
let diffDate, diffTime;
diffDate = (moment(dateOfStart).diff(nowDate) <= 0) && (moment(dateOfEnd).diff(nowDate) >= 0);
if (moment(dateOfStart).diff(nowDate) === 0) {
diffTime = (moment(timeOfStart).diff(nowTime) < 0)
diffDate = diffTime;
}
if (moment(dateOfEnd).diff(nowDate) === 0) {
diffTime = moment(timeOfEnd).diff(nowTime) > 0 diffDate = diffTime;
}
return diffDate;
}