Javascript - Date.now() is not the same - javascript

I want to run a function in a specific time, and found a solution here :Call a javascript function at a specific time of day
Code:
var now = new Date();
var threepm = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 3, 0) - now;
var twelveam = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 1) - now;
if (threepm < 0) {
threepm += 86400000; // it's after 3PM, try 3PM tomorrow.
}setTimeout(threeamfunction, threepm);
But it doesnt work, so I tried printing both now and threepm without deducting the current time.
New Code:
var now = new Date();
var threepm = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 3, 0);
console.log(now);
console.log(threepm);
And I found out that both times aren't the same.
Result of the log is
2020-05-05T19:26:02.913Z
2020-05-05T16:00:03.000Z
Is this normal? Or am I missing something? The reason why my function isn't running because the difference is too big, even when the time set is the same

Okay, so personally I would go with something like a cron job for scheduling something to happen in a specific time or periodically. I suggest you looking at this library, it is awesome and makes your life simple and easy.

Related

Smooth foreground extraction

I am trying to do smooth foreground extraction couple of days.I tried many things but nothing seems to work ;( ;(
I just want smooth human body extraction just like this:https://www.youtube.com/watch?v=rGMqXBvYxog
1-)I use Morphological Transformations and BackgroundSubstraction to do this.
Documentation says
"Opening for removing background noise and closing image for closing small holes inside the foreground objects" But It didn't work ;(
Without Closing and Opening : https://streamable.com/xh368
With Closing and Opening : https://streamable.com/bixmm
Closing and Opening Javascript Code :
let video = document.getElementById('videoInput');
let cap = new cv.VideoCapture(video);
let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16,false);
const FPS = 30;
function processVideo() {
try {
if (!streaming) {
// clean and stop.
frame.delete(); fgmask.delete(); fgbg.delete();
return;
}
let begin = Date.now();
// start processing.
cap.read(frame);
fgbg.apply(frame, fgmask); //Apply Background Substraction
cv.bitwise_not(fgmask,fgmask);//Set background color black and foreground color white for Morphological Transformations
let M = cv.Mat.ones(5,5, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
cv.morphologyEx(fgmask, fgmask, cv.MORPH_OPEN, M, anchor, 1,
cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
cv.morphologyEx(fgmask,fgmask, cv.MORPH_CLOSE, M);
frame.copyTo(fgmask, fgmask); //Copy original colors
cv.imshow('canvasOutput', fgmask);
// schedule the next one.
let delay = 100/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
} catch (err) {
utils.printError(err);
}
};
// schedule the first one.
setTimeout(processVideo, 0);
Full HTML:https://anotepad.com/notes/ne7n4w
All files:https://files.fm/u/c9egsgqe
2-))I am using haarcascades to extract human bodies in this technique , just like this:https://www.bytefish.de/blog/extracting_contours_with_opencv/
This algorithm run on following 5 steps.These algorithm not work very well because sometimes haar cascades cannot detect objects
Javascript Code
let src = cv.imread('canvasInput');
let gray = new cv.Mat();
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
let faces = new cv.RectVector();
let poly=new cv.MatVector();
let faceCascade = new cv.CascadeClassifier();
faceCascade.load('haarcascade_frontalface_default.xml');
let msize = new cv.Size(0, 0);
faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, msize, msize);
//1-Create a mask with the rectangular coordinates
let rect = new cv.Rect(faces.get(0).x, faces.get(0).y,faces.get(0).width,faces.get(0).height);
//2-Mask out
dst = src.roi(rect);
//3-Edge detection using canny
let cannyoutput=new cv.Mat();
cv.Canny(dst, cannyoutput, 0, 100, 3, true);
//4-Find contours
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
cv.findContours(cannyoutput,contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE);
//cv.drawContours();
cv.imshow('canvasOutput', cannyoutput);
src.delete(); gray.delete(); faceCascade.delete();
faces.delete();
contours.delete(); hierarchy.delete();
3-)I tried to apply erosion then dialatios ,it didn't work.This Answer
Without Erosion And Dilation : https://streamable.com/xh368
With Erosion And Dilation : https://streamable.com/fffsn
My javascript code:
let video = document.getElementById('videoInput');
let cap = new cv.VideoCapture(video);
let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16,false);
const FPS = 30;
function processVideo() {
try {
if (!streaming) {
// clean and stop.
frame.delete(); fgmask.delete(); fgbg.delete();
return;
}
let begin = Date.now();
// start processing.
cap.read(frame);
fgbg.apply(frame, fgmask);
cv.bitwise_not(fgmask,fgmask);
let M = cv.Mat.ones(5,5, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
cv.erode(fgmask, fgmask, M, anchor, 1,
cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
cv.dilate(fgmask, fgmask, M, anchor, 1, cv.BORDER_CONSTANT,
cv.morphologyDefaultBorderValue());
frame.copyTo(fgmask, fgmask);
cv.imshow('canvasOutput', fgmask);
// schedule the next one.
let delay = 1000/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
} catch (err) {
utils.printError(err);
}
};
// schedule the first one.
setTimeout(processVideo, 0);
My full html:https://anotepad.com/notes/gg5esk

Difference in time between two dates in javascript?

I am trying to make a countdown clock until the inauguration on January 20, 2017 at approximately 12:00pm using flipclock.js
How do I calculate the difference (in seconds) between the current time and that date/time?
I currently have:
var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = inauguration - now;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
Got it working:
var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
var now = Date.now();
var diff = inauguration.getTime() - now;
diff = diff / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
Currently inauguration is a Date object. You can get the number of seconds by .getTime() function. You need to change:
var diff = inauguration.getTime() - now;
//---------------------^^^^^^^^^^
Your working code should be:
var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
var now = Date.now();
var diff = inauguration.getTime() - now;
diff = diff / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
The idea of getTime() was part of the solution, and along with a couple of other minor changes the above code is now working.
Also, the original implementation works as well:
var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = (inauguration.getTime() - now) / 1000;
var clock = $('#clock').FlipClock(diff, {
countdown: true,
clockFace: "DailyCounter"
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.min.js"></script>
<div id="clock"></div>

How can i make this Audio file only play at the time listed and not any time after?

Below is the code I got from this site. THANK YOU. but everytime the page is loaded it plays the audio file any time after 16:24. Is there a way to prevent this?
var now = new Date();
var audio1 = new Audio('one_minute_remaining-2.mp3');
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 16, 24, 00, 0) -now;
if (millisTill10 < 0)
{
millisTill10 += 1000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function(){audio1.play()}, millisTill10);
var now = new Date();
var audio1 = new Audio('one_minute_remaining-2.mp3');
//Change the hours, minutes, seconds to the time you want it to play
var timeIWantItToPlay = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
16, 24, 00, 0
);
//This is the "exactness" of the time. So if it's within 1 second, it will play
var leeway = 1000;
if ( ( now.getTime() > timeIWantItToPlay.getTime() - leeway ) && ( now.getTime() < timeIWantItToPlay.getTime() + leeway )
{
audio1.play();
}

Changing Date in Flipclock.js

I need help with Flipclock.js I have no knowledge of Jquery and this plugin is driving me nuts. I just need to change the date to 11/23/2014 at 6pm but I can not seem to able to figure that out.
Here is my code
var clock;
$(document).ready(function() {
// Grab the current date
var currentDate = new Date();
// Set some date in the future. In this case, it's always Jan 1
var futureDate = new Date(currentDate.getFullYear() + 1, 0, 1);
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
// Instantiate a coutdown FlipClock
clock = $('.clock').FlipClock(diff, {
clockFace: 'DailyCounter',
countdown: true
});
});
Where you are setting 'futureDate' instead of adding 1 year set the finish time using:
new Date(year, month, day, hour, minute, second, millisecond);
mdn documentation for date
var clock;
$(document).ready(function() {
// Grab the current date
var currentDate = new Date();
// Set some date in the future. ***change to desired date***
//var futureDate = new Date(2014, 11, 23, 6, 0, 0);
var futureDate = new Date(2014, 10, 23, 18, 0, 0); //fixed as per comments
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
// Instantiate a coutdown FlipClock
clock = $('.clock').FlipClock(diff, { clockFace: 'DailyCounter', countdown: true });
});

2 pc with same timezone, but javascript returns different offset

I have 2 laptops A(windows server 2008 R2 enterprise) and B(windows 7 ultimate), both are using timezone (UTC+12:00)Fiji.
I have one file Tz.html with below js:
var tmSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
var so = -1 * tmSummer.getTimezoneOffset();
var tmWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
var wo = -1 * tmWinter.getTimezoneOffset();
alert(so+' '+wo);
I put Tz.html on both laptops, then access it locally via IE.
The popup window on laptop A says: "720 720"
But the popup window on B says: "720 780"
Any clues?
Thanks in advance.

Categories